I'm attempting to code some error alerts into one of my firm's monitoring programs, but I'm not having much luck doing so with return values from the system() command in C. The code invokes an scp transaction to pull a file off another server.....I need it to flag an error if the system is down, or the connection otherwise fails.
I've tried determining what the error return value is with "echo $?" and it looks like 0 is a successful return, then 1 is for anything else, but even coding that into the application it's not picking up a failed attempt. I've just tried getting the return value from the command line, but I don't know whether that value is actually the one returned during execution of the application.
The bottom line is catching when the system() command returns a failure, so I'm not sure if there is a better solution out there.
Thanks, all.
EDIT: Here's the excerpt of code I'm using to identify the error:
if ( system(cmd) != 0 );
{
sftpCrash = TRUE;
printf("FTP crash detected.")
return;
}
Then the int sftpCrash gets returned to a calling function, and executes like so:
if ( sftpCrash == TRUE)
{
Node->color = RED; //Posts an error to our monitoring application
sprintf(reason, "Failure on SFTP connection to %s. Please check server status.",
getenv("HOSTNAME"));
printf(("SFTP crashed. Should post error alert."));
}
This is all being run and executed on a UNIX server.