#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main() {
int res = system("ps ax -o pid -o command | grep sudoku | grep gnome > /dev/null");
printf("res = %d \n", res);
return 0;
}
I want to see if sudoku
is running or not by just examining the return code of system()
(or any other call for that matter). I do not want any output to be printed anywhere.
I do not quite understand the return code of system()
even after looking at the man page
Whether sudoku
is running or not, I get res = 0
.
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 gnome
res = 0
So, since every commands executes successfully, the return code will be 0 :-). You might have better luck with pgrep
and the like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With