Problem solve Get help with specific problems with your technologies, process and projects.

Two ways to determine if a user is signed on

The WRKUSRJOB technique and the method of allocating the user's message queue both work, but each has issues to consider.

You Can View User Feedback To This Tip


John Kohan

There are times when a programmer would like to know when a particular user is signed on. There are two techniques I have seen to accomplish this.

The first is to create a CL program to print the WRKUSRJOB listing and then copy it to a physical file. From the physical file, you can determine if the user is in the listing. This method is effective and works well, but the overhead involved can affect performance.

Example 1:

/* */                                                               
/* Create the report and place into workfile */                     
/* */                                                                
             WRKUSRJOB  USER(&USER) STATUS(*ACTIVE) OUTPUT(*PRINT) + 
                          JOBTYPE(*INTERACT)                         
             CPYSPLF    FILE(QPDSPSBJ) TOFILE(SPOOL)                 
/* */                                                                
/* Read and parse the workfile  */                                         
/* */                                                                
             CALL       PGM(SPOOLR) PARM(&ACTIVE)                    
                                                                     
             DLTOVR     FILE(SPOOL)                                  
                                                     

The second technique I've seen used is to try and allocate the user's message queue. In theory, if the user is signed on you cannot allocate the message queue. Remember that if the job allocating the queue is the same job that allocated the message queue during sign-on, no message is logged. This usually is not a problem because this limits you only from asking the system, "Am I signed on?"

Also, remember to de-allocate the message queue if needed. Although this method has its flaws, the performance over the first example is substantial.

Example 2:

ALCOBJ     OBJ((&USER *MSGQ *EXCL)) WAIT(0) 
MONMSG     MSGID(CPF1002) EXEC(DO)          
RMVMSG     CLEAR(*ALL)                      
GOTO       QUIT                             
ENDDO                                       
DLCOBJ     OBJ((&USER *MSGQ *EXCL))
GOTO       QUIT                    

Try these out the next time you find yourself asking, "Is JSmith signed on?"

-----------------------------------------
About the author: John Kohan is a senior programmer analyst at CT Codeworks.

USER FEEDBACK TO THIS TIP

  • The MSGQ technique is good as far as it goes, but you can't depend on the MSGQ being named the same as the user profile. A safer but probably faster option is the API QUSLJOB (List Jobs). It can be done in CL. You need a user space. You can try this: call qusljob ('USRSPCNAMELIBRARYxxx' 'JOBL0100' '*ALL USERNAMExx*ALL ' '*ACTIVE ' 'I' x'0000000000000000' 'I' x'00000000' x'00000000') This will put all active interactive jobs for USERNAMExx into the user space. Check the header for number of entries -- if > 0, they're on the system. (Check API Programming for how to handle user spaces.) -- Vern Hamberg
  • If you are looking to display the feedback interactively, why not just use: WRKOBJLCK OBJ(USERID) OBJTYPE(*USRPRF). If you get the message "there are no locks for the specified object" you know they're not on the system. The advantage to this is it also traps users who are coming in on Client Access as "QUSER," where their UserID only shows up as the "current user" when you display the job. This way you won't overlook the people who come in client/server or with any other access method. Especially useful when you've got disconnect issues when users don't realize they've got a job still active, and they log back on and lock themselves up. — Linda Ireland

==================================
MORE INFORMATION
==================================

Dig Deeper on iSeries CL programming

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataCenter

Close