• Uncategorized

About c : execvp-is-not-accepting-all-the-argument-list-from-ps–u-command

Question Detail

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).

Question Answer

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);

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.