Hello so I'm trying to read the all the PID from already executing processes with a specific name, saving does PID for later kill does process with a signal alarm every 45 seconds.
The problem comes when executing the execvp with the following parameters:
char* command = "ps"; char* argument_list[] = {"ps ", "-u|grep 'NAME_PROCESS'|grep -v 'grep'|awk '{print $2}'", NULL};
int status_code = execvp(command, argument_list);
The error that shows up is the following:
error: user name does not exist
Usage: ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>' or 'ps --help <s|l|o|t|m|a>' for additional help text.
For more details see ps(1).
You can use shell metacharacters but you need to invoke the shell explicitly and pass the entire command line as a single string (untested)...
const char *command = "sh";
char *argument_list[] = {
"sh", "-c", "ps -u | grep 'NAME_PROCESS' | grep -v 'grep' | awk '{print $2}'", NULL
};
execvp(command, argument_list);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.