Autorun console application in linux - c#

I have developed a console application using MVS and I compiled it so can run on linux.
It runs on linux when I manually run and everything works so far. But whenever I try to run it at boot using crontab it seems not to be working.
My application is a HttpListener. The linux environment I run this is a robot's. I've not used linux very much so I have found and used the below commands to get this run at boot.
#reboot /home/rauman/Downloads/webserver
Then I tried setting a delay of 20secs,
#reboot sleep 20 && /home/rauman/Downloads/webserver
I normally run this application with terminal like below and works fine
./webserver
I'm accessing the robot using putty
After adding this to run at boot, I could see the pid of the application,
pidof webserver
So I guess it runs, but got no permission or something?
I have given permission for the file using,
chmod +x webserver
Any help is appreciated.
Edit : Solved
As, Mr. R pointed out
#reboot sleep 20 && cd /home/rauman/Downloads/ && /.webserver

I ran into the same problem a few days ago. My app runs flawlessly when I call it from its working directory but it failed when getting called from outside. It turned out the app reads a file by a relative address. So I had to change either the program or the pwd.
The easiest way that I came by is changing pwd:
#reboot sleep 20 && cd /home/rauman/Downloads/ && ./webserver

crontab runs processes as root, so any another file (such configurations) beloning to your user profile folder cannot be accessibile by ¢rotab.
Is this you scenario?
If yes, you should them into /etc or into /root.

Related

C# File.exists failing if process started by registry/run

I am making a monitoring application that runs with Windows (Reg key added to HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run to make my app run on Windows Start).
Here's the behavior of my application :
- It logs some informations
- It takes a screenshot
- Upload everything to mysql
- Wait 10 seconds
- Does everything again
When I compile my application in VisualStudio2015, everything works fine.
When I decide to add the autorstart key in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, on Windows start, the application "seems" to freeze just before "Upload everything to mysql", here's the code just before "Upload everything to mysql" :
if (File.Exists(Directory.GetCurrentDirectory() + #"\screenshots\ScreenCapture.jpeg")) {
Is there anything denying my application from using "File.Exists" if the application is started by the registry (Windows) ?
Does my application has the same permissions ?
I have been struggling for 2 days on it and I would really appreciate some help.
Thanks.

Solaris not returning output for certain commands over remote session using Renci.SshNet

I am using a .NET library Renci.SshNet to connect to remote Solaris machine (its a VM on ESXi). It connects fine.
I use the following method to execute the commands and get the Standard Output. This works fine on any Linux machine and almost all command on Solaris (Except few - which is where the issue is)
outstring = sshClient.RunCommand(command).Execute();
For example when command = "cat /etc/release | grep Solaris" -it works fine.
However, when command = "smbios -t SMB_TYPE_SYSTEM" - it doesn't return anything. I try redirecting it to a file. The file gets created - but doesn't have anything on it.
I connect to the system using PuTTY and run the command - it runs perfectly and gives the desired output.
I am perplexed by this behavior. I am using a username with root privilloginto logon. So privileges are ruled out (anyway the same user gets the output in PuTTY).
I am wondering if there is any setting or restriction on Solaris (I am running ver 11.3) which does not allow the smbios command to run like this over a remote connection? Or it is something else? Any guidance will be extremely helpful. If any further info is required, please let me know.
Well, it turns out that it was to do with Path settings. When you login through Putty the $PATH as defined gets set. Hence smbios runs from Putty.
But in a SSH session the $PATH environment variable does not get applied. So its not able to find smbios to run. If you give the full path of smbios - like /usr/sbin/smbios it executes fine over Renci.SshNet.

RegawMOD adb shell su privileges issue

I'm currently building a c# windows app that has a requirement of emptying out the sms inbox from an android devices.
I'm using RegawMOD.android lib to create and execute adb commands.
The first thought was to delete mmssms.db and reboot the device which works.
However, rebooting the device takes a lot of time.
so I took another approach :
I copied the DB to the internal SD card
pulled it to my pc
accessed it with sqlite (through code) and emptied the SMS table.
copied it back to internal SD card.
The problem is with copying it back to /data/data/com.android.providers.telephony/databases/
When I try to run it inside the code, I get permission denied even though I run it with shell and su settings. If I run the same code in adb shell on a command line prompt, it works perfectly.
this is the code :
AdbCommand MoveToData = Adb.FormAdbShellCommand(device, true, "cat", "/storage/sdcard0/download/mmssms.db > /data/data/com.android.providers.telephony/databases/mmssms.db");
In your su 0 cat /storage/sdcard0/download/mmssms.db > /data/data/com.android.providers.telephony/databases/mmssms.db command only the part before the redirection runs with root privileges. The redirection is still being run with your app's user ID.

Process.Start won't work

I am trying to launch a process from a web page's back-end code/app pool. This process will launch an App that i built myself.
For some reason, the process only works / runs when i start it from VS2013... it never works when i launch it from IIS(7.5) itself.
I am on a Windows 7 machine (both IIS host, and App location), and I've setup my web site to only be accessible via internal network.
Here's the code, followed by the config / attempts to fix the issue:
protected void btn_DoIt_Click(object sender, EventArgs e)
{
string file_text = this.txt_Urls.Text;
if (!String.IsNullOrWhiteSpace(file_text))
File.WriteAllText(ConfigurationManager.AppSettings["filePath"], file_text);
ProcessStartInfo inf = new ProcessStartInfo();
SecureString ss = GetSecureString("SomePassword");
inf.FileName = #"........\bin\Release\SomeExecutable.exe";
inf.Arguments = ConfigurationManager.AppSettings["filePath"];
inf.UserName = "SomeUserName";
inf.Password = ss;
inf.UseShellExecute = false;
//launch desktop app, but don't close it in case we want to see the results!
try
{
Process.Start(inf);
}
catch(Exception ex)
{
this.txt_Urls.Text = ex.Message;
}
this.txt_Urls.Enabled = false;
this.btn_DoIt.Enabled = false;
this.txt_Urls.Text = "Entries received and process started. Check local machine for status update, or use refresh below.";
}
Here are the things I've tried to resolve the issue:
Made sure the executing assembly was built with AnyCPU instead of
x86
Ensured that the AppPool that runs the app, also runs under the same account (SomeUsername) as the ProcessStartInfo specified.
Ensured that the specific user account has full access to the executable's folder.
Ensured that IIS_USR has full access to the executable's folder.
Restarted both the app pool and IIS itself many times over implementing these fixes
I am now at a loss as to why this simply will not launch the app... when i first looked into the event log, i saw that the app would die immediately with code 1000:KERNELBASE.dll, which got me on the AnyCPU config instead of X86 fix... that fixed the event log entries but the app still doesn't start (nothing comes up in task manager), and i get no errors in the event log...
if someone could help me fix this problem i would really appreciate it. This would allow me to perform specific tasks on my main computer from any device on my network (phone, tablet, laptop, etc etc) without having to be in front of my main PC...
UPDATE
The comment to my OP, and ultimate answer from #Bradley Uffner actually nailed the problem on the head: My "app" is actually a desktop application with a UI, and in order to run that application, IIS would need to be able to get access to the desktop and the UI, just like if it were a person sitting down in front of the PC. This of course is not the case since IIS is running only as a service account and it makes sense that it shouldn't be launching UI programs in the background. Also see his answer for one way of getting around this.
Your best bet might be to try writing this as 2 parts. A web site that posts commands to a text file (or database, or some other persistent storage), and a desktop application that periodically polls that file (database, etc) for changes and executes those commands. You could write out the entire command line, including exe path command arguments, and switches.
This is the only way I can really think of to allow a service application like IIS to execute applications that require a desktop context with a logged in user.
You should assign a technical user with enough high priviliges to the running application pool. By default the application pool is running with ApplicationPoolIdentity identy which has a very low priviliges.

My C# application is returning 0xE0434352 to Windows Task Scheduler but it is not crashing

I have written a few C# apps that I have running via windows task scheduler. They are running successfully (as I can see from the log files that they are writing ) but windows task scheduler shows them returning a last run result of 0xE0434352. Is there something I need to do in my C# application so that it returns a success code to the windows task scheduler?
Another option is to simply use the Application log accessible via the Windows Event Viewer. The .Net error will be recorded to the Application log.
You can see these events here:
Event Viewer (Local) > Windows Logs > Application
When setup a job in new windows you have two fields "program/script" and "Start in(Optional)". Put program name in first and program location in second.
If you will not do that and your program start not in directory with exe, it will not find files that are located in it.
Hans Passant was correct, I added a handler for AppDomain.CurrentDomain.UnhandledException as described here http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception(v=vs.71).aspx I was able to find the exception that was occurring and corrected it.
I was referencing a mapped drive and I found that the mapped drives are not always available to the user account that is running the scheduled task so I used \\IPADDRESS instead of MAPDRIVELETTER: and I am up and running.
In case it helps others, I got this error when the service the task was running at didn't have write permission to the executable location. It was attempting to write a log file there.
I had this issue and it was due to the .Net framework version. I had upgraded the build to framework 4.0 but this seemed to affect some comms dlls the application was using. I rolled back to framework 3.5 and it worked fine.
I got the same error but I have fixed it by changing the file reading path from "ConfigFile.xml" to AppDomain.CurrentDomain.BaseDirectory.ToString() + "ConfigFile.xml"
In my case, this error due to file path error because task manager starts program from "System32" as initial path but the folder we thought.
I was getting the same message message within dotNet Core 2.2 using MVC 5, however nothing was being logged to the Windows Event Viewer.
I found that I had changed the Project sdk from Microsoft.NET.Sdk.Web to Microsoft.NET.Sdk.Razor (seen within the projects.csproj file). I changed this back and it worked fine :)
In my case it was because I had message boxes. Once I commented that code out, it started working. I remembered that could be a problem when I looked at the event log as suggested in this thread. Thank you everyone!
I encountered this problem when working with COM objects. Under certain circumstances (my fault), I destroyed an external .EXE process, in a parallel thread, a variable tried to access the com interface app.method and a COM-level crash occurred. Task Scheduler noticed this and shut down the app. But if you run the app in the console and don't handle the exception, the app will continue to work ...
Please note that if you use unmanaged code or external objects (AD, Socket, COM ...), you need to monitor them!
Also message box in PowerShell. I converted PowerShell script to exe. When running as admin it's worked but in task schedule I received this error also.
There was an line in PowerShell script with write-output. After commented this line and compile new exe Task Schedule was completed successfully.
It is permission issue in my case the task scheduler has a user which doesn't have permission on the server in which the database is present.

Categories