kill process and get the output c# - c#

Hi am creating a console application that executes another windows console application.
Sometimes the application never end its execution, an I need to force kill it, if exceeded a time. When the application is force killed I am unable to get the output of the application.
There is a way to get the output before killing the running process in order to write to a file or get it after killing it?
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "robocopy.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.Arguments = parameters;
process.Start();
//set the maximum allowed time in which the robocopy lives
if (process.WaitForExit(15000))
{
logInfo = process.StandardOutput.ReadToEnd();
//do something else
}
else
{
process.Kill();
logInfo = process.StandardOutput.ReadToEnd();
//write the output to a file
}

Related

How to Wait Until an Operation Finishes?

I have C# code which included Python code in which it is run through CMD codes in C#. When the Python code is run the operations are done, a JSON file is created and then it will be opened in C#. In this situation, how the C# code can wait to check if the output of Python (data.json) is created or not, and just when the output is created, the rest of C# code is allowed to be run:
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("F:\\");
process.StandardInput.WriteLine("cd F:\\Path");
process.StandardInput.WriteLine("python Python_Code.py");
process.StandardInput.Flush();
process.StandardInput.Close();
process.WaitForExit();
Then, the generated data with Python will be retrieved:
string Output_Python = File.ReadAllText(#"Data.json");
JavaScriptSerializer Ser = new JavaScriptSerializer();
Predicted Output = Ser.Deserialize<Predicted>(Output_Python);
You don't need to go through cmd.exe. The Python interpreter itself is an executable; in other words, it can be started and executed directly. The arguments for the Python interpreter (like the path+name of the script to be executed) and desired working directory can be set through the appropriate Process.StartInfo properties:
Process process = new Process();
process.StartInfo.FileName = "python.exe";
process.StartInfo.Arguments = "Python_Code.py";
process.StartInfo.WorkingDirectory = #"F:\Path";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
Now you only need to wait for the Python interpreter to exit (which means it finished executing the python script)
process.WaitForExit();
and after the Python process has exited, simply check if the json file exist/has been written:
if (System.IO.File.Exists(pathToJsonFile))
{
... do stuff with json file ...
}
else
{
... json file does not exist, something went wrong...
}
Side note: I kept process.StartInfo.RedirectStandardOutput = true; in my code example here, since i don't know what your program will really do. However, unless your program wants to process the output of the script that normally appears in a console window, setting RedirectStandardOutput to true is not necessary.
You should have a look at the FileSystemWatcher class. Documentation here.
Then you can do something like this:
using (FileSystemWatcher watcher = new FileSystemWatcher())
{
watcher.Path = YourDirectory;
// Watch for changes in LastWrite time
watcher.NotifyFilter = NotifyFilters.LastWrite;
// Watch for the wanted file
watcher.Filter = "data.json";
// Add event handlers.
watcher.Created += WhateverYouWantToDo;
}
You can check to see if the file data.json is finished being written to its output folder (Code from this answer):
private bool IsFileLocked(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if (stream != null) stream.Close();
}
//file is not locked return
return false;
}

Application is not exiting C#

I am working with an C# console application in which I am creating process and when I killing that process it shows that process got killed but process does not get stopped and also application does not exit.
process.StartInfo.WorkingDirectory = #"C:\Users\rajgau\Documents\logstash-2.1.1\bin";
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.StandardInput.WriteLine("logstash -f logstash-filename1.conf");
Thread.Sleep(1000 * 10); // atleast some data get loaded
process.StandardInput.Close();
Console.WriteLine("{0} is active: {1}", process.Id, !process.HasExited);
process.Kill();
Console.WriteLine("{0} is active: {1}", process.Id, !process.HasExited);
Also For exiting the console application I tried Environment.Exit(0) but even it did not help.Kindly suggest some points.
You are starting a command shell process (cmd.exe) and then also creating child processes (logstash) that you would also need to kill.
You'll need to kill the process tree such as recommended in the answer here https://stackoverflow.com/a/23845431/87464

C#: Calling a php script and beeing able to stop it

I am currently working on a C# Program which needs to call a local PHP script and write its output to a file. The problem is, that I need to be able to stop the execution of the script.
First, I tried to call cmd.exe and let cmd write the output to the file which worked fine. But I found out, that killing the cmd process does not stop the php cli.
So I tried to call php directly, redirect its output and write it from the C# code to a file. But here the problem seems to be, that the php cli does not terminate when the script is done. process.WaitForExit() does not return, even when I am sure that the script has been fully executed.
I cannot set a timeout to the WaitForExit(), because depending on the arguments, the script may take 3 minutes or eg. 10 hours.
I do not want to kill just a random php cli, there may be others currently running.
What is the best way to call a local php script from C#, writing its output to a file and beeing able to stop the execution?
Here is my current code:
// Create the process
var process = new System.Diagnostics.Process();
process.EnableRaisingEvents = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "php.exe";
// CreateExportScriptArgument returns something like "file.php arg1 arg2 ..."
process.StartInfo.Arguments = CreateExportScriptArgument(code, this.content, this.options);
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.RedirectStandardOutput = true;
// Start the process or cancel, if the process should not run
if (!this.isRunning) { return; }
this.currentProcess = process;
process.Start();
// Get the output
var output = process.StandardOutput;
// Wait for the process to finish
process.WaitForExit();
this.currentProcess = null;
To kill the process I am using:
// Mark as not running to prevent starting new
this.isRunning = false;
// Kill the process
if (this.currentProcess != null)
{
this.currentProcess.Kill();
}
Thanks for reading!
EDIT
That the cli does not return seems to be not reproducible. When I test a different script (without arguments) it works, probably its the script or the passing of the arguments.
Running my script from cmd works just fine, so the script should not be the problem
EDIT 2
When disabling RedirectStandardOutput, the cli quits. could it be, that I need to read the output, before the process finishes? Or does the process wait, when some kind of buffer is full?
EDIT 3: Problem solved
Thanks to VolkerK, I / we found a solution. The problem was, that WaitForExit() did not get called, when the output is not read (probably due to a full buffer in the standard output). My script wrote much output.
What works for me:
process.Start();
// Get the output
var output = process.StandardOutput;
// Read the input and write to file, live to avoid reading / writing to much at once
using (var file = new StreamWriter("path\\file", false, new UTF8Encoding()))
{
// Read each line
while (!process.HasExited)
{
file.WriteLine(output.ReadLine());
}
// Read the rest
file.Write(output.ReadToEnd());
// flush to file
file.Flush();
}
Since the problem was that the output buffer was full and therefore the php process stalled while waiting to send its output, asynchronously reading the output in the c# program is the solution.
class Program {
protected static /* yeah, yeah, it's only an example */ StringBuilder output;
static void Main(string[] args)
{
// Create the process
var process = new System.Diagnostics.Process();
process.EnableRaisingEvents = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "php.exe";
process.StartInfo.Arguments = "-f path\\test.php mu b 0 0 pgsql://user:pass#x.x.x.x:5432/nominatim";
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.RedirectStandardOutput = true;
output = new StringBuilder();
process.OutputDataReceived += process_OutputDataReceived;
// Start the process
process.Start();
process.BeginOutputReadLine();
// Wait for the process to finish
process.WaitForExit();
Console.WriteLine("test");
// <-- do something with Program.output here -->
Console.ReadKey();
}
static void process_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
{
if (!String.IsNullOrEmpty(e.Data)) {
// edit: oops the new-line/carriage-return characters are not "in" e.Data.....
// this _might_ be a problem depending on the actual output.
output.Append(e.Data);
output.Append(Environment.NewLine);
}
}
}
see also: https://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline%28v=vs.110%29.aspx

Problem invoking external .exe using c#

I'm trying to run this .exe file from my c# code, it does call the .exe file but then it crashes midway through. If I click on the .exe on the explorer it does its job, so I wonder if there's a problem with the code I'm using to invoke it:
string fileName = "loadscript.exe";
Utils.Logger.Info("Calling script:" + fileName);
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = fileName;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
Thread.Sleep(10000);
process.WaitForExit();
int exitCode = process.ExitCode;
string output = process.StandardOutput.ReadToEnd();
Utils.Logger.Info(".exe Output: ");
Utils.Logger.Info(output);
Thread.Sleep(10000);
process.WaitForExit();
int exitCode = process.ExitCode;
string output = process.StandardOutput.ReadToEnd();
It seems to me like this is creating a deadlock and this might be the problem for the eventual crash. Remove the sleep and try this instead:
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
int exitCode = process.ExitCode;
Please see the answer to this question for an explanation:
ResGen.exe stucks when redirect output
process.StartInfo.UseShellExecute = false;
That requires you to specify the name of a .exe file. With it set to true, another Windows function is used to start the file, it is smart enough to figure out that a .bat file requires cmd.exe to be started to interpret the commands in the .bat file.
Which is what you need to do yourself now, the FileName must be "cmd.exe", the Arguments property needs to be "loadscript.bat".

How can you start a process from asp.net without interfering with the website?

We have an asp.net application that is able to create .air files.
To do this we use the following code:
System.Diagnostics.Process process = new System.Diagnostics.Process();
//process.StartInfo.FileName = strBatchFile;
if (File.Exists(#"C:\Program Files\Java\jre6\bin\java.exe"))
{
process.StartInfo.FileName = #"C:\Program Files\Java\jre6\bin\java.exe";
}
else
{
process.StartInfo.FileName = #"C:\Program Files (x86)\Java\jre6\bin\java.exe";
}
process.StartInfo.Arguments = GetArguments();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.PriorityClass = ProcessPriorityClass.Idle;
process.Start();
string strOutput = process.StandardOutput.ReadToEnd();
string strError = process.StandardError.ReadToEnd();
HttpContext.Current.Response.Write(strOutput + "<p>" + strError + "</p>");
process.WaitForExit();
Well the problem now is that sometimes the cpu of the server is reaching 100% causing the application to run very slow and even lose sessions (we think this is the problem).
Is there any other solution on how to generate air files or run an external process without interfering with the asp.net application?
Cheers,
M.
The problem is here: process.WaitForExit();, you are simply halting the execution of the application. You might want to use a thread to start the process and some sort of IPC (Inter Process Communication, like remoting, named pipes) to know when the generation is finished.

Categories

Resources