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 ?
Advertisement
Answer
When you add “|” to a bash line the bash interpreter splits the command in two processes and feeds the output from one to the other, when you call that command using Process it sends the argument as is.
The most close you can achieve is to start yourself the two process and feed one with the output of the other through the input/output streams.
About part 2 of your question, if the program runs as privileged then Process.GetProcesses will list all running processes on the system included daemons.