How to find if an IBM i (as400) program is interactive or batch – RPG API example 

 January 28, 2009

By  NickLitten

Any program can easily find out the mode it is running in by calling an IBM supplied *API – which will return the status of the job we are running. Basically, we just have to teach our program to shout “Hey Dude – am I running in batch or what?” and then the dark lord IBMi will answer “My son, you are running interactive! Dont be resource greedy… and don’t call me Dude

So, lets go through an example of a simple Service program to do this. The module below, INTERACT, determines whether your program is running in interactive or batch mode:

RPG Code Snippet interactive or batch
H NoMain D #Interactive PR N D DummyPrm 1 Options( *Omit ) P #Interactive B Export D #Interactive PI N D DummyPrm 1 Options( *Omit ) * Local variables D RetdInfDs DS D 4 D 10I 0 Inz( %Size( RetdInfDs ) ) D RiJobType 61 61 D RiTypeInteractive... D C 'I' D LenRetdInf S 10I 0 Inz( %Size( RetdInfDs ) ) D FmtName S 8 Inz( 'JOBI0100' ) D QlJobName S 26 Inz( '*' ) D IntJobId S 16 Inz( *Blank ) * MAINLINE C Call 'QUSRJOBI' C Parm RetdInfDs C Parm LenRetdInf C Parm FmtName C Parm QlJobName C Parm IntJobId C If RiJobType = RiTypeInteractive C Return *On C Else C Return *Off C EndIf P #Interactive E

Here are the steps for installing and using #Interact:

  • Upload #INTERACT to a source physical file with a record length of 112 bytes. Give the uploaded member a name of #INTERACT and a PDM type of RPGLE.
  • Use option 15 in PDM to compile #INTERACT.
  • Issue the command: CrtSrvPgm SrvPgm( #INTERACT ) Export( *All ) ActGrp( WhateverYouWant )

To use this service program from an RPG IV program, follow these steps:

Include the following prototype in your source with the D-specs:

D #Interactive PR N
D DummyPrm 1 Options( *Omit )

If you expect to use this procedure in more than one program, you might consider using a /Copy member for this prototype.

Here are a couple of sample calls to #INTERACT in the C-specs:

C If #Interactive( *Omit )
C 'Interactive' Dsply DummyVar
C EndIf
C If Not #Interactive( *Omit )
C 'Batch' Dsply
C Endif

Compile it and Use it šŸ™‚

Use PDM option 15 to compile your program.

Issue the following command to create your program (Note that the following assumes your program is a standalone program, i.e., a single module, named MYPGM):

CrtPgm MyPgm Module( MYPGM ) BndSrvPgm( INTERACT ) ActGrp( Whatever )

Adapted from a utility by Mike Cravitz


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day. Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
__CONFIG_colors_palette__{"active_palette":0,"config":{"colors":{"cff50":{"name":"Main Accent","parent":-1},"a344d":{"name":"Accent Transparent","parent":"cff50"}},"gradients":[]},"palettes":[{"name":"Default","value":{"colors":{"cff50":{"val":"var(--tcb-skin-color-0)"},"a344d":{"val":"rgba(46, 138, 229, 0.85)","hsl_parent_dependency":{"h":210,"l":0.54,"s":0.78}}},"gradients":[]},"original":{"colors":{"cff50":{"val":"rgb(0, 178, 255)","hsl":{"h":198,"s":1,"l":0.5}},"a344d":{"val":"rgba(0, 178, 255, 0.85)","hsl_parent_dependency":{"h":198,"s":1,"l":0.5}}},"gradients":[]}}]}__CONFIG_colors_palette__

Get In Touch

Iā€™m always looking for awesome input, feedback and critique!