It is a surprisingly common misconception that you cannot debug a program that runs in batch. While you have to perform some extra steps, which identify the job you want to debug within, the debugging interface is identical to that of a interactive debug.

To be able to run source debug, whether interactive or batch, you need to create (compile) your CLLE, RPGLE, and SQLRPGLE objects with the appropriate debug view parameter. To learn about the various debug views see the post Debug views finding your favorite.

First I need to submit the job to batch, and hold it. I can either hold the job queue the job is submitted to, or submitting the job as held using the following command:

SBMJOB CMD(CALL PGM(TESTPGM1)) JOB(TEST1) JOBQ(QBATCH) + HOLD(*YES)

I need to find the job name and number of the submitted job. The easiest way I know to do this is to use the Work User Job command, WRKUSRJOB , to look at all the jobs I have submitted to job queues:

WRKUSRJOB STATUS(*JOBQ)

The "Work with User Jobs" screen displayed, and I use option 5 to display the job's details:

Work with User Jobs Type options, press Enter. 5=Work with Opt Job User Type -----Status----- 5 TEST1 SIMON BATCH JOBQ HELD

At the top of the next screen, "Work with Job" is the information I need:

Work with Job Job: TEST1 User: SIMON Number: 192561

At any command line I need to start the Service Job with the job's details I just discovered:

STRSRVJOB JOB(192561/SIMON/TEST1)

Then I start debug. I want to debug TESTPGM2, which is called by TESTPGM1, the program I submitted to batch:

STRDBG PGM(*LIBL/TESTPGM2) UPDPROD(*YES)

The debug screen is shown, but I cannot enter any break points. I press F12 to exit the debug screen and return to the command line.

I now release the job or the job queue, depending upon which I held at the start. As the released job becomes active I am presented with the following screen:

Start Serviced Job Job: TEST1 User: SIMON Number: 192561 The serviced job has been released from the job queue. Press Enter to start the job or F10 to enter debug commands for that job.

I press F10 and I am taken to the "Command Entry" screen (like with QCMD). On the command line I type DSPMODSRC and press Enter. The source code for the program I am debugging is displayed. I can now enter break points.

I press F3 or F12 to return to the "Command Entry" screen. And I press F3 or F12 to return to the "Start Service Job" screen. I press Enter to start the job, and I debug the program as I would an interactive program.

When I am done I must end debug and the Service Job using the following commands:

ENDDBG ENDSRVJOB

Yes, it is that easy.

You can learn more about debugging in batch from the IBM website here.

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