Monitoring for Errors in PHP Script Called from CLLE

I recently created an IBM i command for our company’s use that involved calling a PHP script from a CLLE program using QShell. In the event that the PHP script encountered an error I wanted a way for that script to report the error back to the calling CLLE. There are a few different ways to do that, but here is how I handled it.

In the PHP script, at the spots where I knew problems could happen, I used a try/catch to identify the problem and set a status value:

$exitStatus = 0;

try {
// code that might generate an error
} catch ( Exception $e ) {
$exitStatus = 1;

At the end of the PHP script I use the following:

exit( $exitStatus );

A successful execution of the script returns a status of 0, while a trapped error returns a positive value, in this case 1. If I have multiple places in the script that could generate errors, I can repeat that code with other positive integer values for $exitStatus, making it possible to identify the specific error.

When the script ends and QShell returns control to the calling CLLE, QShell returns a completion message: QSH0005 (from message file QZSHMSGF in library QSHELL). The message text is normally “Command ended normally with exit status 0.” When you use the “exit” statement as I did and return a value other than zero (0), that value is reflected in the message, as “Command ended normally with exit status 1.”

Since QSH0005 is not an escape message, I can’t use a MONMSG to check for the error. If I set environment variable QIBM_QSH_CMD_ESCAPE_MSG to a value of ‘Y’, any exit status greater than 0 will be reported as an escape message, which could then be monitored for. I chose not to go that route.

Instead I chose to receive the QSH005 message and compare the message text with the normal message to see if an error occurred. If there was an error I then sent CPF9897 as an escape message with the same message text. I can then monitor for CPF9897 in the CLLE’s calling program and process as needed. Here are the steps in the CLLE:



IF  COND(&MSGID *EQ ‘QSH0005’ *AND &RTNTYPE *EQ ‘01’ *AND %TRIM(&MSG) *NE ‘Command ended normally with exit status 0.’)  THEN(DO)

Thanks to several Club Seiden members for their input, especially Kevin Adler, who pointed me to the “exit” statement.

Discuss in our community

0 comment

Leave a Reply