There have been numerous requests from programmers who want to access and manipulate with a job's QTEMP library. It is used for debugging and/or recovering from a job that has fallen over with a MSGW status. On occasion, the remedy would involve either investigating the contents in QTEMP or modifying the contents in it. Or the error may be also caused by an incorrect library list. The program may attempt to access objects that do not exist due to a missing library in the library list. Whichever the case, it is especially useful if we could remotely execute commands on a job as if it was the job itself that issued the command. Once the problem has been rectified, such as the missing library list scenario, then instead of answering "c" to cancel the request, you now answer "r" to retry the operation.
There have been earlier approaches to execute commands on another session that uses the message queue concept. It places the workstation message queue into *BREAK delivery mode and uses a break-handling program. When a message is received, the break handling program takes over and executes the command contained in the message. The problem with this approach is that it will not work with batch jobs. You cannot send messages to a batch job from another job. Moreover, the user profile also needs to be setup to run an initial program that will place the workstation message queue into break delivery mode. This approach is cumbersome and of limited use with only interactive jobs.
Finally there is a way to run commands on any job, batch or online and without any cumbersome pre-requisites. It consists of three parts, a command (RUNJOBCMD), a command processing program (SNDCMDCL) and an exit program (RCVCMDCL).
For this to work, you must have the following:
1. *USE authority to the user profile where the command is intended to be executed.
2. *USE authority to STRSRVJOB, ENDSRVJOB and TRCJOB commands. These commands are normally shipped with *EXCLUDE authority.
3. Or have *SERVICE special authority in your user profile.
Code
This is the RUNJOBCMD command. It accepts two main parameters, the iSeries 400 job (where the command will be executed) and a command to be executed. Note: The command will be executed under the job that is being specified. It does not run under your current sign on session.
/* Copyright 2002 David Ong (dsso@hotmail.com) */
/* Command.....: RUNJOBCMD */
/* Description.: Runs a command on a job */
/* Compile.....: CRTCMD CMD(QGPL/RUNJOBCMD) PGM(QGPL/SNDCMDCL) + */
/* SRCFILE(YOURLIB/QCMDSRC) */
/* */
CMD PROMPT('Run job command')
PARM KWD(JOBNAME) TYPE(*CHAR) LEN(10) MIN(1) +
CHOICE('Name') PROMPT('Job name')
PARM KWD(JOBUSER) TYPE(*CHAR) LEN(10) MIN(1) +
CHOICE('Name') PROMPT('Job user')
PARM KWD(JOBNUMBER) TYPE(*CHAR) LEN(6) +
RANGE('000000' '999999') MIN(1) +
CHOICE('000000 - 999999') PROMPT('Job +
number')
PARM KWD(COMMAND) TYPE(*CHAR) LEN(512) MIN(1) +
CHOICE('Name') PROMPT('Command to run')
C This is the SNDCMDCL program which is the CPP for RUNJOBCMD. It will interrupt the iSeries 400 job by starting a service request. Then sends the command to be executed via data queues. When the command finishes executing, the messages (if any) are returned and displayed to the user.
/* Copyright 2002 David Ong (dsso@hotmail.com) */
/* Program.....: SNDCMDCL */
/* Description.: CPP for RUNJOBCMD. Sends command to execute */
/* via data queue. */
/* Compile.....: CRTCLPGM PGM(QGPL/SNDCMDCL) + */
/* SRCFILE(YOURLIB/QCLSRC) */
/* */
PGM PARM(&JOBNAME &USER &JOBNO &CMD)
DCL VAR(&JOBNAME) TYPE(*CHAR) LEN(10)
DCL VAR(&USER) TYPE(*CHAR) LEN(10)
DCL VAR(&JOBNO) TYPE(*CHAR) LEN(06)
DCL VAR(&CMD) TYPE(*CHAR) LEN(512)
DCL VAR(&MSG) TYPE(*CHAR) LEN(132)
DCL VAR(&LEN) TYPE(*DEC) LEN(5 0) VALUE(80)
DCL VAR(&SNDDTAQ) TYPE(*CHAR) LEN(10)
DCL VAR(&RCVDTAQ) TYPE(*CHAR) LEN(10)
DCL VAR(&WAIT) TYPE(*DEC) LEN(5 0) VALUE(15)
MONMSG MSGID(CPF0000) EXEC(GOTO CMDLBL(ERROR))
/* Creates data queue to send command and receive messages */
CHGVAR VAR(&SNDDTAQ) VALUE('SND' || &JOBNO)
CHGVAR VAR(&RCVDTAQ) VALUE('RCV' || &JOBNO)
CHKOBJ OBJ(&SNDDTAQ) OBJTYPE(*DTAQ)
MONMSG MSGID(CPF9801) EXEC(DO)
CRTDTAQ DTAQ(QGPL/&SNDDTAQ) MAXLEN(512) TEXT('Send +
Command Data Queue')
CRTDTAQ DTAQ(QGPL/&RCVDTAQ) MAXLEN(512) TEXT('Status +
Data Queue')
ENDDO
/* Clear data queue just in case & sends the command to run */
CALL PGM(QCLRDTAQ) PARM(&SNDDTAQ 'QGPL')
CALL PGM(QCLRDTAQ) PARM(&RCVDTAQ 'QGPL')
CALL PGM(QSNDDTAQ) PARM(&SNDDTAQ 'QGPL' &LEN &CMD)
/* Interrupts the job and executes the command */
STRSRVJOB JOB(&JOBNO/&USER/&JOBNAME)
TRCJOB SET(*ON) MAXSTG(1024) EXITPGM(QGPL/RCVCMDCL)
/* Wait for indication that the command has completed processing */
CALL PGM(QRCVDTAQ) PARM(&RCVDTAQ 'QGPL' &LEN &MSG
&WAIT)
DLYJOB DLY(2)
TRCJOB SET(*END) MAXSTG(1024)
/* End servicing job and interrupt */
ENDSRVJOB
/* Receive the completion messages */
CHGVAR VAR(&WAIT) VALUE(0)
RCVMSG: CALL PGM(QRCVDTAQ) PARM(&RCVDTAQ 'QGPL' &LEN &MSG
&WAIT)
IF COND(&LEN *EQ 0) THEN(GOTO CMDLBL(CLEANUP))
SNDPGMMSG MSG(&MSG)
CHGVAR VAR(&MSG) VALUE(' ')
GOTO CMDLBL(RCVMSG)
/* Error occurred within this program, so just send error message*/
ERROR: RCVMSG MSGTYPE(*EXCP) RMV(*NO) MSG(&MSG)
SNDPGMMSG MSG(&MSG)
CLEANUP: DLTDTAQ DTAQ(QGPL/&SNDDTAQ)
MONMSG MSGID(CPF0000)
DLTDTAQ DTAQ(QGPL/&RCVDTAQ)
MONMSG MSGID(CPF0000)
ENDIT: ENDPGM
This is the RCVCMDCL program. It is the exit program specified in the TRCJOB command. This program will run under the job being serviced. It accepts the commands to execute via data queues and returns the completion messages also through data queues.
/* Copyright 2002 David Ong (dsso@hotmail.com) */
/* Program.....: RCVCMDCL */
/* Description.: Receives command from data queue and */
/* excecutes it via QCMDEXC */
/* Compile.....: CRTCLPGM PGM(QGPL/RCVCMDCL) + */
/* SRCFILE(YOURLIB/QCLSRC) */
/* */
PGM PARM(&DUMMY)
DCL VAR(&DUMMY) TYPE(*CHAR) LEN(100)
DCL VAR(&JOBNO) TYPE(*CHAR) LEN(06)
DCL VAR(&CMD) TYPE(*CHAR) LEN(512)
DCL VAR(&MSG) TYPE(*CHAR) LEN(132)
DCL VAR(&CLEN) TYPE(*DEC) LEN(15 5)
DCL VAR(&SNDDTAQ) TYPE(*CHAR) LEN(10)
DCL VAR(&RCVDTAQ) TYPE(*CHAR) LEN(10)
DCL VAR(&LEN) TYPE(*DEC) LEN(5 0)
DCL VAR(&WAIT) TYPE(*DEC) LEN(5 0) VALUE(0)
MONMSG MSGID(CPF0000) EXEC(GOTO CMDLBL(ENDIT))
RTVJOBA NBR(&JOBNO)
CHGVAR VAR(&RCVDTAQ) VALUE('SND' || &JOBNO)
CHGVAR VAR(&SNDDTAQ) VALUE('RCV' || &JOBNO)
/* Receive command from data queue */
CALL PGM(QRCVDTAQ) PARM(&RCVDTAQ 'QGPL' &LEN &CMD +
&WAIT)
/* Runs the command */
IF COND(&LEN *EQ 0) THEN(GOTO CMDLBL(ENDIT))
CHGVAR VAR(&CLEN) VALUE(&LEN)
CALL PGM(QCMDEXC) PARM(&CMD &CLEN)
MONMSG MSGID(CPF0000)
/* Indicates command completion and sends completion messages */
CHGVAR VAR(&LEN) VALUE(1)
SENDMSG: CALL PGM(QSNDDTAQ) PARM(&SNDDTAQ 'QGPL' &LEN &MSG)
RCVMSG RMV(*NO) MSG(&MSG) MSGLEN(&LEN)
IF COND(&MSG *NE ' ') THEN(GOTO CMDLBL(SENDMSG))
ENDIT: ENDPGM
Here I provide a hypothetical example of displaying the contents of QTEMP for a batch job named GLPOST, running under userid DAVIDONG and the job number is 123456.
RUNJOBCMD JOBNAME(GLPOST) JOBUSER(DAVIDONG) JOBNUMBER(123456) COMMAND('DSPOBJD OBJ(QTEMP/*ALL) OBJTYPE(*ALL) OUTPUT(*PRINT)')
Because the command runs under Davidong's user profile, then you will need to work with Davidong's spool files to view the report.