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
Related
I made a launcher for my ARK Server where I can easily change my settings and start.
I start the server with this:
Process serverprocess = new Process();
serverprocess.StartInfo.FileName = Path.GetFileName(serverpath);
serverprocess.StartInfo.Arguments = launch;
serverprocess.StartInfo.UseShellExecute = false;
serverprocess.Start();
serverprocess.WaitForExit();
But when I press CTRL+C it doesn't wait until the ARK Server stops, it still runs in background while my app is killed.
I did some testing, the server recieves the shutdown signal and stops (which takes some time, espeically when the server isn't fully started). But when it takes time to stop the server is still running while my app already closed.
Doublepressing CTRL+C will kill the server but since the first press already brings me out of the console I'm unable to doublepress.
Any idea how I can prevent my app from beeing closed while still stopping the ARK server?
Adding Console.TreatControlCAsInput = true; will stop the server from even recieving the signal.
Thank you!
My test is ok
Process proc = new Process();
proc.StartInfo.FileName = "/bin/bash";//sh
proc.StartInfo.Arguments = "-c \" " + command + " && echo 'OK'\"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
You should properly close your program for it to close properly your server. Here you go:
var serverProcess = Process.Start(new ProcessStartInfo
{
FileName = Path.GetFileName(serverpath),
Arguments = launch,
UseShellExecute = false
});
while (Console.ReadLine() != "exit")
{
Thread.Sleep(500);
}
serverProcess.Close();
In C#, I am trying to run a command line program that gets input from stdin and then it returns the output to stdout . I need to keep cmd.exe running (using /k) and in a for loop, send in text and then wait for the output before sending the next text. This works if I don't redirect stdout but not after I redirect it. Initially I got data back from stdout (although much later) and now that is no longer working other than the initial call to start the program. This link says "Alternately, you can avoid the deadlock condition by creating two threads and reading the output of each stream on a separate thread."
Would that fix my issue and if so, how would I do that?
The code is set up as follows:
StringBuilde sb = new StringBuilder();
Process process = new Process();
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = workingdirectory;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/k";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.EnableRaisingEvents = true;
process.OutputDataReceived += (sender, args) => AppendData(args.Data); //this appends the output to a stringbuilder
process.ErrorDataReceived += (sender, args) => AppendError(args.Data);
process.Start();
//sw is a streamwriter
sw= process.StandardInput;
//now call the command line code
sw.WriteLine(" some.exe some.arg ");
process.BeginOutputReadLine();
foreach(DataRow row in dtMydata.Rows)
{
mytext=row["Text"].toString();
sw.WriteLine(mytext);
sw.WriteLine(Environment.NewLine); //This redirects the text to the program,
}
I have a C# program that launches a child process and captures its output in a string. This works on most Windows machines (Windows 7 and newer), but when Kaspersky anti-virus is present, Process.StandardOutput.ReadToEnd() returns null. There is no error code or exception. The child process is a trusted console application. The process takes 5 or 6 seconds to run.
The code for launching the child process is as follows:
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = "icao.exe";
psi.Arguments = im_path + "image.jpg";
Process p = new Process();
p.StartInfo = psi;
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
MessageBox.Show(error);
p.WaitForExit();
int exitCode = p.ExitCode;
MessageBox.Show(exitCode+"");
Why does output end up being null when Kaspersky is present?
My guess is that Kaspersky's heuristics are seeing that your program wants to execute another exe. Because nothing is telling Kaspersky that this is ok, it flags your program as possible malware, because it wants to interface with other programs that are developed by other companies. If you are able to I would try white listing your program with Kaspersky and see if that solves your issue.
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
}
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.