Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return Status for system()

Status
Not open for further replies.

fox12

Programmer
Jan 18, 2005
62
0
0
US
Gurus,

In a code segment, I have the code below

strcat(cmdstring, "/export/home/user9/scripts/trans");
// trans is an executable script

status = system(cmdstring);
if (status = -1) {
sprintf(msg, "System() call failed\n");
}
else
{
sprintf(msg, "System() call succeeded\n");
}

My debugging message showed "System() call succeeded". That means the script "trans" was called and executed. However, the "trans" script did not output any debug message. Once "trans" is executed, it should write a "transaction.log" file to the directory /export/home/user9/log. But I got nothing by calling "system()". Is there a way I can check further the status for calling "system()"?

Thanks so much.

Fox12

 
What OS are you using?
What is the value of status after system() executes?
 
The OS is Solaris 10 x86. Don't know the actual value of the return status. But I know it is not equal to -1. I'll let you know tomorrow.

Thanks.
 
> if (status = -1)
It's hard to tell what is happening when you don't post the code you're running. You've used assignment here, not comparison, so there is no way this code is ever going to print success.

Post actual code in [code][/code] tags, otherwise it's just guessing.

> status = system(cmdstring);
Read the manual page perhaps?
Phrases like
"returns the exit status of the shell in the format specified by waitpid(3C)."

So leading to you need to use say WEXITSTATUS(stat) to recover the actual exit status of your call.

--
 
cmdstring = "cd /usr/local/transaction/reports/; /export/home/user9/scripts/trans purchase_08112006.log"
//purchase_08112006.log is the actual log file to be
//passed to the script "trans" and to be created
//by "trans" .

int status;
status = system(cmdstring);
if (status == -1) {
syslog(LOG_ERR, strerror(errno));
}
else {
syslog(LOG_ERR, "trans successfully completed");
}

What I saw is that status = 0 and the message "trans successfully completed".

Since I did not find any "purchase_08112006.log" file created, I would assume that the "trans" script did not execute. But this is conflicting with the msg I saw.

Besides, I can manually execute the "trans" script successfully.

What may be wrong?

Thanks

Fox12

 
Because -1 only means the system() call itself FAILED completely in an utterly miserable fashion.

Any other result means system() did what it was supposed to do (start another process), and the result contains the exit status of the process which was run.

Since you seem to neither use code tags, nor read manual entries, how about
Code:
    int status;
    status = system(cmdstring);
    if (status == -1) {
      syslog(LOG_ERR, strerror(errno));
    }
    else {
      status = WEXITSTATUS(status);
      syslog(LOG_ERR, "trans completed with status=%d", status);
    }

Now you should get the same results as if you typed at the prompt
[tt]
$ cd /usr/local/transaction/reports/
$ /export/home/user9/scripts/trans purchase_08112006.log
$ echo $?
[/tt]

--
 
Does the trans script print anything to the screen when it runs? If not, try adding an echo statement at the beginning of the script.

If you run:
"cd /usr/local/transaction/reports/; /export/home/user9/scripts/trans purchase_08112006.log" on the command line (without the quotes) does it run correctly, or just if you run the statements one at a time?
 
The status still returned a value "0" by using status = WEXITSTATUS(status).

How can I go further?

Fox12
 
By the way, if I run "cd /usr/local/transaction/reports/; /export/home/user9/scripts/trans purchase_08112006.log", it works fine.

Fox12
 
> The status still returned a value "0" by using status
If you type in a deliberately broken "trans" command (say the file doesn't exist, or you don't specify a file at all), and then [tt]echo $?[/tt] do you get meaningful status returns as well?

I notice that you're passing a command sequence to system() rather than a command.
What system("cmd") does is in effect
[tt]sh -c cmd[/tt]
But since you have a ; in there, maybe the ; is terminating the whole sh line. All it does is a "cd" (which succeeds) and then returns without ever having run the trans command.

How about
Code:
char *cmdstring =
  "\"cd /usr/local/transaction/reports/;"
  "/export/home/user9/scripts/trans purchase_08112006.log\"";
status = system(cmd);

So rather than seeing
[tt]sh -c cd ... ; trans ...[/tt]
it sees
[tt]sh -c "cd ... ; trans ..."[/tt]
The extra double quotes protect the ; from being interpreted in the wrong place.

--
 
That is a good suggestion. I'll try that once I am back in office Monday.

Thanks so much.
 
I tried what Salem suggested in the following two ways, and found that both of the status is "1" now.

char *cmdstring =
"\"cd /usr/local/transaction/reports/;"
"/export/home/user9/scripts/trans purchase_08112006.log\"";
status = system(cmd);
status = WEXITSTATUS(status);

and

char *cmdstring =
"\"cd /usr/local/transaction/reports/;
/export/home/user9/scripts/trans purchase_08112006.log\"";
status = system(cmd);
status = WEXITSTATUS(status);

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top