I am trying to run bash command with pipe using mono process start
here is code snippet I am trying
single commands works, however pipe commands fails to run, what am I missing here ?
ProcessStartInfo oInfo = new ProcessStartInfo(command, args);
oInfo.UseShellExecute = false;
oInfo.CreateNoWindow = true;
oInfo.RedirectStandardOutput = true;
oInfo.RedirectStandardError = true;
StreamReader srOutput = null;
StreamReader srError = null;
Process proc = System.Diagnostics.Process.Start(oInfo);
proc.WaitForExit();
I tried running "ps -aux" which runs fine. However ps -aux | grep gnome command failed.
I tried these scenarios scenario 1: command = ps argument = -aux | grep gnome
scenario 2: command = ps argument = "-c ' -aux | grep gnome ' "
scenario 3 :
command = ps argument = " -c \" -aux | grep gnome \" "
all these failed with
error: garbage option
Usage: ps [options]
Try 'ps --help ' or 'ps --help ' for additional help text.
Also on side question, the reason I am trying to do this is to figure out of a particular daemon is already running. Is there a standard way to get this info. for instance in windows we can query running services using ServiceController.GetServices().
Is something similar available on mono/Linux directly ?
答案 0 :(得分:1)
当您添加" |"对于bash行,bash解释器在两个进程中拆分命令并将输出从一个进程转发到另一个进程,当你使用Process调用该命令时它会按原样发送参数。
你可以实现的最接近的就是从两个进程开始,通过输入/输出流将另一个进程输出到另一个进程。
关于问题的第2部分,如果程序以特权运行,那么Process.GetProcesses将列出包含守护程序的系统上所有正在运行的进程。