-3

Let's assume I am running a unix command using system("foocmd param1") on c++. If foocmd returns "Invalid argument" back to the terminal via stderr, then how do I get my c++ code to know whether foocmd failed?

Here is my attempted solution:

My assumption is that I should check whether anything got returned to stderr by calling the command. To do that, I tried switching over to popen. Currently, this is the way I check. I first output my stderr into a file.

sprintf(cmd, "foocmd param1 2>temp.txt");
system(cmd);

Then I check if temp.txt is empty or not.

But there has to be a better way. Can anyone lend me a hand?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
CuriousKimchi
  • 205
  • 2
  • 10
  • 5
    `system` returns a status code. – Hatted Rooster Oct 05 '16 at 12:38
  • 3
    [Read the manual page](http://man7.org/linux/man-pages/man3/system.3.html). – Some programmer dude Oct 05 '16 at 12:40
  • 2
    Possible duplicate of [return value of system() in C](http://stackoverflow.com/questions/8654089/return-value-of-system-in-c) – Hayt Oct 05 '16 at 12:42
  • @GillBates Doesn't it seem like a hassle to figure out what exit code I am looking for? From what I just read, it seems like there is no rule for the exit code, and it depends on how it's coded. That seems to mean that there can be multiple exit codes even though all are errors? – CuriousKimchi Oct 05 '16 at 12:50
  • 1
    The documentation for the command you run should document the possible exit codes. If not then you can assume it follows standard conventions, that zero is okay and anything non-zero either means that the `system` function failed or that the command failed (depending on if `system` return a positive or negative value). Basically like if you ran the command from a shell. – Some programmer dude Oct 05 '16 at 12:55
  • You do it the same as you do in C - the library is the same. – Toby Speight Oct 05 '16 at 14:02

1 Answers1

2

The usual way is to examine the return value of system():

  • If it's zero, the command executed successfully and exited with a status of 0.
  • If it's negative, there was a problem with system() itself, and you can't assume anything.
  • If it's positive, then you can use WEXITSTATUS() and related macros to find out how the process exited.

See the system(3) man page for the full details.


Most of the time, you are only interested in whether the command says it succeeded:

if (!system(cmd)) {
    syslog(LOG_WARNING, "Command \"%s\" failed", cmd);
    /* maybe some more error handling here */
    goto err_return;
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • Doesn't it seem like a hassle to figure out what exit code I am looking for? From what I just read, it seems like there is no rule for the exit code, and it depends on how it's coded. That seems to mean that there can be multiple exit codes even though all are errors? – CuriousKimchi Oct 05 '16 at 12:50
  • 2
    Most of the time, it's sufficient to simply test against zero: `if (!system(cmd)) { /* error handling */ }` which isn't much hassle at all. You only pay in complexity when you need it. – Toby Speight Oct 05 '16 at 13:48