In brief
CL (Control Language) is the IBM i command language used to write control programs: they call RPG programs in sequence, prepare files, set the library list, submit jobs to a queue and react to errors. Business logic usually lives in RPG or SQL, while CL handles the order of steps and the state of the job. In maintenance, most trouble comes from MONMSG error handling, hardcoded library names and job logs that do not tell you what happened.
What CL is and what it is for
CL is the same language an operator types on the command line. Every command name combines a verb and an object, for example CRTLIB (create library), DLTF (delete file), WRKACTJOB (work with active jobs). A CL program is simply a sequence of such commands compiled into a *PGM object, with variables, conditions and loops added.
In a typical IBM i system, CL plays three roles:
- run control: the nightly end-of-day run that clears work files, calls several RPG programs in order and prints a report;
- environment setup: setting the library list, overriding a file with OVRDBF, changing job attributes;
- operations: taking a save, submitting a job to a queue, notifying the operator with a message.
The boundary is simple. CL is good at control and bad at computation. When a CL program starts doing calculations, looping over file records and checking dozens of conditions, that part belongs in RPG or an SQL query. We cover modern RPG in the article RPG today: free-form and modern tools.
OPM and ILE: two kinds of CL programs
Older systems contain both kinds, and it pays to tell them apart whenever you make a change.
| Feature | OPM CL | ILE CL |
|---|---|---|
| Source type | CLP | CLLE |
| Compile command | CRTCLPGM | CRTBNDCL, or CRTCLMOD plus CRTPGM |
| Calling other modules | CALL a program | CALL a program and CALLPRC a procedure |
| Combining with ILE RPG | only through CALL | in one program or service program |
New code is written in ILE CL. Converting an old program from CLP to CLLE usually comes down to changing the source type and recompiling, but you need to check for behavioral differences, for example around activation groups. The next change to a given program is a good moment for it, rather than a separate project for all of them at once.
Newer language constructs improve readability a lot: DOWHILE, DOUNTIL and DOFOR instead of GOTO jumps, SELECT with WHEN instead of nested IFs, SUBR subroutines called with CALLSUBR, integer variables with TYPE(*INT). In code written years ago, loops built from labels and GOTO are the main reason nobody wants to touch it.
A control program with error handling
The most important command in any control program is MONMSG. According to IBM documentation, it monitors escape, notify and status messages. A MONMSG placed right after the declarations applies to the whole program, while one placed after a specific command applies only to that command.
PGM PARM(&DAY)
DCL VAR(&DAY) TYPE(*CHAR) LEN(8)
DCL VAR(&COUNT) TYPE(*DEC) LEN(10 0)
/* Any unhandled error ends the program in one place */
MONMSG MSGID(CPF0000) EXEC(GOTO CMDLBL(ERROR))
CLRPFM FILE(SETTLE/RESULTS)
CALL PGM(SETTLE/CALC) PARM(&DAY)
RTVMBRD FILE(SETTLE/RESULTS) NBRCURRCD(&COUNT)
IF COND(&COUNT *EQ 0) THEN(DO)
SNDPGMMSG MSGID(CPF9898) MSGF(QCPFMSG) +
MSGDTA('No results for day' *BCAT &DAY) +
MSGTYPE(*ESCAPE)
ENDDO
RETURN
ERROR:
SNDPGMMSG MSGID(CPF9898) MSGF(QCPFMSG) +
MSGDTA('Run aborted, check the job log') +
MSGTYPE(*ESCAPE)
ENDPGMThe program ends with an escape message both when a command fails and when the result is empty. That way the scheduler or the calling program sees a failure, not a false success.
The most common mistake in old code is the opposite: a global MONMSG MSGID(CPF0000) without the EXEC parameter, which means “ignore every error and carry on.” Such a run completes normally even when the calculation program never started. When reviewing CL programs, searching for exactly this pattern is a good first step.
Running programs: job queues and the scheduler
Control programs rarely run interactively. The SBMJOB command submits a batch job to a job queue, from which a subsystem such as QBATCH picks it up. In SBMJOB you set the user profile, the library list and the job log level. The parameter LOG(4 00 *SECLVL) writes all messages with their second-level text to the job log, which makes analyzing a nightly failure much easier.
The built-in IBM i job scheduler is handled by ADDJOBSCDE (add an entry) and WRKJOBSCDE (review entries). An entry defines the command, frequency, days and time. The built-in scheduler does not handle complex dependencies between jobs, so larger installations build dependencies into the CL program itself (one control program calls the steps in order) or use a separate scheduling tool. For a comparison with the mainframe approach, see the article JCL and job scheduling on the mainframe.
SQL services help you review the state: the QSYS2.SCHEDULED_JOB_INFO view shows scheduler entries, and the QSYS2.JOBLOG_INFO table function returns a job log as rows. A report of “which nightly jobs ended in error” becomes a single query instead of browsing spooled output.
Maintaining old CL code
CL programs live in a system longer than RPG programs, because hardly anyone changes them. After years, a single run may consist of a dozen CL programs calling one another. A few rules bring order to such code without risk:
- Inventory. A list of CL programs with who calls them: the scheduler, a menu, another program. The DSPPGMREF command helps, as does the approach in the article Documenting a legacy system when its authors are gone.
- Library list instead of hardcoded names. A command with a library written into the code (SETTLE/CALC) only works in production. A call without a qualifier or with *LIBL lets the same program run in a test environment.
- Explicit error handling. Every program ends with an escape message on error, and every MONMSG without EXEC has a comment explaining why.
- Source in git. CL compiles the same way as RPG, so it goes into the same repository and the same automated build. See the article Git and CI/CD for IBM i.
- Run tests. A control program is tested by running the whole job stream against a test library and comparing results with a baseline, as described in the article on automated tests for IBM i applications.
This kind of cleanup is typical work for a maintenance team. The project on a maintenance team for a public administration system shows an example.
Questions and answers
What is the difference between CL and RPG?
CL controls the system: it calls programs and manages files, jobs and messages. RPG processes data: it reads records, calculates and writes results. Both languages can be combined in one ILE program. In a well-built job stream, CL is responsible for the order of steps and the reaction to errors, while each computational step is a separate RPG program or SQL query.
How do I run a CL command from an RPG program or from SQL?
From RPG, the simplest way is the QCMDEXC API, which takes the command text and its length. In SQL, use the QSYS2.QCMDEXC procedure, which you can call from any tool that connects to the database, including from outside IBM i. Keep in mind that the command runs with the authority of the job that calls it.
Why does my CL program complete normally when a step did not run?
Most often because of a global MONMSG MSGID(CPF0000) or CPF9999 without the EXEC parameter. That statement catches every error and lets the program continue, so the scheduler sees success. The fix is to route the error to a label that sends an escape message. The error details are in the job log (DSPJOBLOG or QSYS2.JOBLOG_INFO).
Is it worth rewriting old CL programs?
Rarely in full. It pays to rewrite a program you are changing anyway, one that contains business logic, or one nobody understands because of GOTO loops. For the rest, it is enough to move them to git, fix error handling and remove hardcoded library names. These are small changes, but they reduce the risk of nightly failures.