Reputation: 767
I am using linux system command to kill some processes in c file. I just want to know the different return values that are possible. I dont get clear idea when i searched the net. the following command i am using in c.
ret = system("pkill raj");
Upvotes: 1
Views: 3515
Reputation: 703
System will return the exit status of pkill unless the call to system fails. pkill returns these somewhat ridiculous values,
EXIT STATUS
0 One or more processes matched the criteria.
1 No processes matched.
2 Syntax error in the command line.
3 Fatal error: out of memory etc.
which preclude their use to kill processes by pattern if the current running state of the process is unknown. For example, if you run 'pkill raj' and get a return value of 1, it is because pkill failed to match 'raj' not because it failed to kill the raj process, which will result in a return value of 0 with an error message on stderr. I would recommend against the use of pkill for this reason. Use pgrep to match process names, and once you have a pid, use kill to kill that pid.
Upvotes: 0
Reputation: 2491
from the man page:
The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case /bin/sh could not be executed, the exit status will be that of a command that does exit(127).
So you need to check WEXITSTATUS(ret) for the return value of your pkill command.
code sample:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int status;
if(( status = system("kill -9 13043")) != -1){
fprintf(stdout, "kill command exit status: %d\n", WEXITSTATUS(status));
}
return 0;
}
Upvotes: 3