Wednesday, February 2, 2022

Finding which job submitted a batch job

which job submitted this one

An occasional question I get is there a way to determine which job submitted a particular batch job. Fortunately by using a SQL table function I can retrieve that information.

To be able to get to retrieve this information the batch job must be active, running, if it not what is described here will not work.

Before I starting showing SQL I need a program that will submit a job to batch.

01  DCL VAR(&JOBTYPE) TYPE(*CHAR) LEN(1)

02  RTVJOBA TYPE(&JOBTYPE)

03  IF COND(&JOBTYPE = '1') THEN(CALLSUBR SUBR(INTERACTIV))
04  ELSE CMD(CALLSUBR SUBR(BATCH))
    /*===================================================*/
05  SUBR SUBR(INTERACTIV)
06    SBMJOB CMD(CALL PGM(PGM01)) JOB(SIMONS_JOB)
07  ENDSUBR
    /*===================================================*/
08  SUBR SUBR(BATCH)
09    DLYJOB DLY(600)
10  ENDSUBR

I am sure of you are saying: "That looks like a CL program, but where are the PGM and ENDPGM commands?"

For several releases the PGM and ENDPGM commands have not been required in a CL program, unless you are passing parameters to the program. At work I do keep the PGM and ENDPGM in place as not having them in the source code has freaked out some other programmers.

Line 2: The job type tells me if the program is running interactive, "1", or in batch, "0". I need this piece of information for the program to submit itself to batch.

Line 3: I do like using subroutines in CL as it makes it easier to follow the logic in complex programs. Do I really need to explain what is happening on this line?

Lines 5 – 7: In the interactive job subroutine the program submits to batch a call to itself, with the job name SIMONS_JOB. It is important that the job has a unique job so that I can easily find it in a later step.

Lines 8 – 10: When the job is running in batch it will "pause" for 10 minutes. The only reason I have this step is I can find it using the examples code below.

After compiling the source code, I call the program. The program waits in a delay status.

Now I can start using SQL to find the job using the JOB_INFO table function. I have written about it before, therefore, I am not going to go into much detail about it. One of the columns returned in the results set for this table function is a column called SUBMITTER_JOB_NAME, this will contain the name of the job that submitted this one to batch.

01  SELECT JOB_NAME,
02         SUBMITTER_JOB_NAME
03    FROM TABLE(QSYS2.JOB_INFO(JOB_TYPE_FILTER => '*BATCH'))
04   WHERE JOB_NAME_SHORT = 'SIMONS_JOB'

Lines 1 and 2: These are the only two columns I am interested in.

Line 3: By using the job type filter any search I perform is limited to just batch jobs.

Line 4: If you have IBM i 7.4 TR5 or 7.3 TR11 there is a column that just contains the job name part of the Job Name. This makes it easier for me to search for the job's name, SIMONS_JOB.

If you do not have those Technology Refreshes applied to your IBM i partition you'll have to use the following wild card search:

04   WHERE JOB_NAME LIKE '%SIMONS_JOB%'

No matter which version of the Where clause I use I get the same results:

JOB_NAME                 SUBMITTER_JOB_NAME
-----------------------  -----------------------
050224/SIMON/SIMONS_JOB  050221/SIMON/QPADEV0003

Can I get more information about the submitting job? If it is still active, or it generated a job log, I can use the JOBLOG_INFO table function. I have written about it before, therefore, I will not be describing my example code in great detail:

01  SELECT TO_LIBRARY AS "Library",
02         TO_PROGRAM AS "Program",
03         TO_MODULE AS "Module",
04         TO_PROCEDURE AS "Procedure",
05         MESSAGE_TEXT AS "Message text"
06    FROM TABLE(QSYS2.JOBLOG_INFO('050221/SIMON/QPADEV0003'))
07   WHERE MESSAGE_TEXT LIKE '%JOB(SIMONS_JOB)%'

Line 6: This is the full job name of the job that submitted my CL program to batch.

Line 7: I only want the result row that contain the submit job command for the job SIMONS_JOB.

My results give me more interesting information:

Library  Program  Module  Procedure  Message text
-------  -------  ------  ---------  -------------------------------------
MYLIB    PGM01    PGM01   PGM01      600 - SBMJOB CMD(***) JOB(SIMONS_JOB)

Now I know not only the name of the job that submitted my program, but also the name of the program too.

 

This article was written for IBM i 7.4, and should work for some earlier releases too.

3 comments:

  1. You can combine both queries to conduct some particular analysis pretty fast.
    i.e. return the last message text log entry of all active batch jobs in the system

    SELECT JOB_NAME, MESSAGE_TEXT
    FROM TABLE(QSYS2.ACTIVE_JOB_INFO(JOB_NAME_FILTER => '*ALL')) T1
    CROSS JOIN LATERAL
    (SELECT * FROM TABLE(QSYS2.JOBLOG_INFO(JOB_NAME))
    ORDER BY ORDINAL_POSITION DESC
    FETCH FIRST 1 ROW ONLY)
    WHERE JOB_TYPE = 'BCH'

    ReplyDelete
  2. Simon, great read . These are great research tools/ functions.

    ReplyDelete
  3. Good to know, re 7.4, sadly doesn't look available on 7.3 but one to remember.

    ReplyDelete

To prevent "comment spam" all comments are moderated.
Learn about this website's comments policy here.

Some people have reported that they cannot post a comment using certain computers and browsers. If this is you feel free to use the Contact Form to send me the comment and I will post it for you, please include the title of the post so I know which one to post the comment to.