return code of system()
First of all, you should be using WEXITSTATUS(res)
. The standard clearly states:
If command is not a null pointer, system() shall return the termination status of the command language interpreter in the format specified by waitpid().
I suspect the problem is that the command actually succeeds (grep finds itself). Try not to redirect the output for a moment:
[cnicutar@fresh ~]$ ./test 989 sh -c ps ax -o pid -o command | grep sudoku | grep gnomeres = 0
So, since every commands executes successfully, the return code will be 0 :-). You might have better luck with pgrep
and the like.
The way you are trying to capture the output of grep
may not work.
Based on the post:C: Run a System Command and Get Output?
You can try the following. This program uses popen()
#include <stdio.h>#include <stdlib.h>int main( int argc, char *argv[] ){ FILE *fp; int status; char path[1035]; /* Open the command for reading. */ fp = popen("/bin/ps -x | /usr/bin/grep gnome-sudoku", "r"); if (fp == NULL) { printf("Failed to run command\n" ); exit; } /* Read the output a line at a time - output it. */ while (fgets(path, sizeof(path)-1, fp) != NULL) { printf("%s", path); } pclose(fp);return 0;}
For reference to popen() see:
http://linux.die.net/man/3/popen
And if you try to use grep
then you can probably redirect the output of grep
and read the file in the following way:
#include <stdlib.h>#include <string.h>#include <stdio.h>int main() { int res = system("ps -x | grep SCREEN > file.txt"); char path[1024]; FILE* fp = fopen("file.txt","r"); if (fp == NULL) { printf("Failed to run command\n" ); exit; } // Read the output a line at a time - output it. while (fgets(path, sizeof(path)-1, fp) != NULL) { printf("%s", path); } fclose(fp); //delete the file remove ("file.txt"); return 0;}
If you have pgrep
, use it instead of your shell pipeline.
system("pgrep -x gnome-sudoku >/dev/null");
When you call
system("ps ax -o pid -o command | grep sudoku | grep gnome > /dev/null");
the system executes
sh -c 'ps ax -o pid -o command | grep sudoku | grep gnome > /dev/null'
which shows up in ps
and passes the grep
filters.