c# running daemon command line - c#

I have several batch files that I need to execute using C#, the problem is at the end of the first batch file it is running a daemon process. This is causing the second batch file to never be executed because of the WaitForExit(). I can not take this out because I dont know how much time the first batch file need to run (until it hits running the daemon)
BatchFile1.bat ->
BatchFile2.bat ->
BatchFile3.bat ->
BatchFile4.bat
inside BatchFile1.bat, I have blah, blah, blah and at the end it is running a daemon process which of course will never exit
//not exit , even if there is error
public void Run_Process(string process_name, string s)
{
Process myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/C " + process_name + s;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.RedirectStandardError = true;
myProcess.Start();
string standard_output = myProcess.StandardOutput.ReadToEnd();
myProcess.WaitForExit();
Console.WriteLine(standard_output);
}

To Execute a cmd.exe this might work:
static void ExecuteCommand(string command)
{
int exitCode;
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
// *** Redirect the output ***
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
process = Process.Start(processInfo);
process.WaitForExit();
// *** Read the streams ***
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
exitCode = process.ExitCode;
Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output));
Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error));
Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
process.Close();
}
static void Main()
{
ExecuteCommand("echo testing");
}

Related

PsExec why don't I get full output of remote command

Why do I NOT get 3 lines of output from running TestApp remotely? In string 'output' I get license info plus "Start#" line, BUT not the next two lines:
public static string RunPsExec()
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\Users\Vence\Downloads\PSTools\PsExec.exe";
p.StartInfo.Arguments = #"\\10.215.yy.yy -u xxxxxx -p ""xxxxxx"" C:\Projects\TestApp\TestApp\bin\Debug\TestApp.exe";
p.Start();
string output = p.StandardOutput.ReadToEnd();
string errormessage = p.StandardError.ReadToEnd();
p.WaitForExit();
return output;
}
TestApp:
static void Main(string[] args)
{
Console.WriteLine("Start# " + DateTime.Now.ToShortTimeString());
System.Threading.Thread.Sleep(5000);
Console.WriteLine("Middle# " + DateTime.Now.ToShortTimeString());
System.Threading.Thread.Sleep(5000);
Console.WriteLine("End# " + DateTime.Now.ToShortTimeString());
}
One solution that I see is set RedirectStandardInput to false, since it is not needed in this example.
However, if you would like to pass the input to the TestApp in a future (which requires RedirectStandardInput=true), one solution might be to use PAexec (https://www.poweradmin.com/paexec/) as an alternative to PsExec. According to these question (Can't receive asynchronous output from psexec when launching application remotely), PAExec writes directly to console buffer, instead of stout/sterr. To make it work, you would have to read the output asynchronously, like this:
public static string RunPsExec()
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = #"C:\<path_to>\PaExec.exe";
p.StartInfo.Arguments = #\\10.215.yy.yy -u xxxxxx -p ""xxxxxx"" ""C:\Projects\TestApp\TestApp\bin\Debug\TestApp.exe"" ";
p.OutputDataReceived += (sender, args) => Display(args.Data);
p.Start();
p.BeginOutputReadLine();
p.WaitForExit(10000);
return;
}
static void Display(string output)
{
Console.WriteLine(output);
}

Run WinSat Command C#

I am trying to run cmd winsat -drive c programmatically
The code below is only returning "Windows System Assessment Tool" the first line of the output but is not letting the winsat run and return the rest of the output
I am looking for the entire output to be returned shown here
The code I am using is this
public string RunAndOutput(object command)
{
var procStartInfo =
new ProcessStartInfo("winsat", "/c " + "-drive c");
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
var proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(); // Wait for everything to finish
return result;
}
You will need to wait for the process to exit using WaitForExit(), like this:
public string RunAndOutput(object command)
{
var procStartInfo =
new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
var proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(); // Wait for everything to finish
return result;
}

C# Start Process on Mac - FFMPEG - Exit Code 1

I am trying to start a process on Mac and Windows (using Unity) to run FFMPEG to convert a video to a .ogv video. My code is as follows:
string command = "ffmpeg -i '" + filepath + "' -codec:v libtheora -qscale:v 10 -codec:a libvorbis -qscale:a 10 -y '"+workingDir+"/ogv_Video/"+System.IO.Path.GetFileNameWithoutExtension(filepath)+".ogv'";
UnityEngine.Debug.Log("Command: "+command);
try{
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo (workingDir+"/..", command);
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.FileName =workingDir+"/ffmpeg";
//Process.Start (startInfo);
Process p = Process.Start(startInfo);
p.EnableRaisingEvents = true;
string strOutput = p.StandardOutput.ReadToEnd();
UnityEngine.Debug.Log ("Running..."+strOutput);
p.WaitForExit();
UnityEngine.Debug.Log ("Got here. "+strOutput);
int exitCode = p.ExitCode;
UnityEngine.Debug.Log ("Process exit code = "+exitCode);
}
catch(Exception e) {
UnityEngine.Debug.Log ("An error occurred");
UnityEngine.Debug.Log ("Error: "+e);
}
The command executes and does not through any exception. However, it terminates instantly and prints Exit Code 1 which is "Catchall for general errors" -this seems not too helpful!
What am I doing wrong with my code, please?
You'll notice that my code prints out the command in full. If I copy that command and paste it into the terminal, it runs absolutely fine.
It turns out I was setting up the arguments wrongly. Referring to this Stack Overflow question, I was able to produce the expected result with the following code:
try{
Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.FileName = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) +#"ffmpeg";
process.StartInfo.Arguments = command;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
JSONDataObject rtnMsg = new JSONDataObject("StartConvertOK", "-1", new List<string>());
return JsonUtility.ToJson(rtnMsg);
}
It does seem as though the answer was not that different from what I was doing, but it does work!

Reading output from another running application

I'm working on a custom IDE in C# for a scripting language, and I have a problem.
I'm trying to start the compiler process (pawncc.exe) and pass arguments to it. I've done that, and now I have a problem. When I want to display the output from the compiler application, it only displays some parts of it. It should output this (got this from the command prompt):
Pawn compiler 3.2.3664 Copyright (c) 1997-2006, ITB CompuPhase
newGM.pwn(0) : fatal error 100: cannot read from file: "includes/main_include.inc"
Compilation aborted.
1 Error.
But it doesn't. It outputs this (in the application, using the same command/arguments):
Pawn compiler 3.2.3664 Copyright (c) 1997-2006, ITB CompuPhase
1 Error.
I just don't get it! It's a really weird thing. It might be something simple but I've been looking at it, and researching for hours now! Here's my code:
public Form3(string path)
{
InitializeComponent();
this._path = path;
Process myProcess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("pawncc.exe");
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.Arguments = path + " -r -d2";
myProcess.StartInfo = startInfo;
myProcess.Start();
while (true)
{
string myString;
byte[] buffer = new byte[512];
var ar = myProcess.StandardOutput.BaseStream.BeginRead(buffer, 0, 512, null, null);
ar.AsyncWaitHandle.WaitOne();
var bytesRead = myProcess.StandardOutput.BaseStream.EndRead(ar);
if (bytesRead > 0)
{
myString = Encoding.ASCII.GetString(buffer, 0, bytesRead);
}
else
{
myProcess.WaitForExit();
break;
}
richTextBox1.Text = myString;
}
}
!!EDIT:
It does the same thing with this code:
public Form3(string path)
{
InitializeComponent();
this._path = path;
Process myProcess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("pawncc.exe");
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.Arguments = path + " -r -d2";
myProcess.StartInfo = startInfo;
myProcess.Start();
using (StreamReader reader = myProcess.StandardOutput)
{
string result = reader.ReadToEnd();
richTextBox1.Text = result;
}
}
You need to redirect the standard error stream as well:
startInfo.RedirectStandardError = true;
Edit: I just reviewed the code and discovered that you are only readonly the StandardOutput stream.
I generally monitor the process for both the standard and error output streams using the DataReceived events on the process and adding the results into a stringbuilder, then storing the StringBuilder content in the UI element:
private static System.Text.StringBuilder m_sbText;
public Form3(string path)
{
InitializeComponent();
this._path = path;
Process myProcess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("pawncc.exe");
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.Arguments = path + " -r -d2";
myProcess.StartInfo = startInfo;
m_sbText = new System.Text.StringBuilder(1000);
myProcess.OutputDataReceived += ProcessDataHandler;
myProcess.ErrorDataReceived += ProcessDataHandler;
myProcess.Start();
myProcess.BeginOutputReadLine();
myProcess.BeginErrorReadLine();
while (!myProcess.HasExited)
{
System.Threading.Thread.Sleep(500);
System.Windows.Forms.Application.DoEvents();
}
RichTextBox1.Text = m_sbText.ToString();
}
private static void ProcessDataHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
// Collect the net view command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
// Add the text to the collected output.
m_sbText.AppendLine(outLine.Data);
}
}
There are obviously variations on this, but this should get you started.
I dont have the pawnCC application so I cant try but it appears they restrict the verbosity of debugging information to external applications - apart from the command prompt.
Can you try spawning the pawncc.exe via cmd:
"cmd.exe \c CommandParameterToLaunchPawnCCwithArguments"
I've noticed some sporadic issues when dealing with the raw output/error streams from spawned processes in the past, hence why I usually deal with captured output via eventing:
Process myProcess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo("pawncc.exe");
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.Arguments = path + " -r -d2";
myProcess.EnableRaisingEvents = true;
myProcess.OutputDataReceived += OnOutputDataReceived;
myProcess.ErrorDataReceived += OnErrorDataReceived;
myProcess.StartInfo = startInfo;
myProcess.Start();
myProcess.BeginOutputReadLine();
myProcess.BeginErrorReadLine();

Silent installation

I am writing a InstallerClass using C# as a custom action for my installer, and I can successfully run an external exe (installation) using the InstallerClass, but when I try to use /quiet in the InstallerClass, it does not install the exe. But I can successfully install this in silent mode using /quiet in the command prompt.
Is there any reason for this or otherwise how to install in silent mode using C#?
Following is the code I use within the Commit method (overriden):
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = pathExternalInstaller;
p.StartInfo.Arguments = "/quiet";
p.Start();
Here is what I use to do a quiet Install and Uninstall:
public static bool RunInstallMSI(string sMSIPath)
{
try
{
Console.WriteLine("Starting to install application");
Process process = new Process();
process.StartInfo.FileName = "msiexec.exe";
process.StartInfo.Arguments = string.Format(" /qb /i \"{0}\" ALLUSERS=1", sMSIPath);
process.Start();
process.WaitForExit();
Console.WriteLine("Application installed successfully!");
return true; //Return True if process ended successfully
}
catch
{
Console.WriteLine("There was a problem installing the application!");
return false; //Return False if process ended unsuccessfully
}
}
public static bool RunUninstallMSI(string guid)
{
try
{
Console.WriteLine("Starting to uninstall application");
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", string.Format("/c start /MIN /wait msiexec.exe /x {0} /quiet", guid));
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = Process.Start(startInfo);
process.WaitForExit();
Console.WriteLine("Application uninstalled successfully!");
return true; //Return True if process ended successfully
}
catch
{
Console.WriteLine("There was a problem uninstalling the application!");
return false; //Return False if process ended unsuccessfully
}
}
This works for me.
Process process = new Process();
process.StartInfo.FileName = # "C:\PATH\Setup.exe";
process.StartInfo.Arguments = "/quiet";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
Have you tried using the /Q or /QB parameter that is listed in the Installation parameters? It might look something like this:
p.StartInfo.Arguments = "/Q";
I got that out of this document: http://msdn.microsoft.com/en-us/library/ms144259(v=sql.100).aspx
Here is my logic to silent install an app for all users:
public void Install(string filePath)
{
try
{
Process process = new Process();
{
process.StartInfo.FileName = filePath;
process.StartInfo.Arguments = " /qb ALLUSERS=1";
process.EnableRaisingEvents = true;
process.Exited += process_Exited;
process.Start();
process.WaitForExit();
}
}
catch (InvalidOperationException iex)
{
Interaction.MsgBox(iex.Message, MsgBoxStyle.OkOnly, MethodBase.GetCurrentMethod().Name);
}
catch (Exception ex)
{
Interaction.MsgBox(ex.Message, MsgBoxStyle.OkOnly, MethodBase.GetCurrentMethod().Name);
}
}
private void process_Exited(object sender, EventArgs e)
{
var myProcess = (Process)sender;
if (myProcess.ExitCode == 0)
// do yours here...
}
string filePath = #"C:\Temp\Something.msi";
Process.Start(filePath, #"/quiet").WaitForExit();
It worked for me.

Categories

Resources