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.
/* */ /* Create the report and place into workfile */ /* */ WRKUSRJOB USER(&amp;amp;USER) STATUS(*ACTIVE) OUTPUT(*PRINT) + JOBTYPE(*INTERACT) CPYSPLF FILE(QPDSPSBJ) TOFILE(SPOOL) /* */ /* Read and parse the workfile */ /* */ CALL PGM(SPOOLR) PARM(&amp;amp;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.
ALCOBJ OBJ((&amp;amp;USER *MSGQ *EXCL)) WAIT(0) MONMSG MSGID(CPF1002) EXEC(DO) RMVMSG CLEAR(*ALL) GOTO QUIT ENDDO DLCOBJ OBJ((&amp;amp;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.
- 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
- Need to know who's signed on? This command uses the WRKUSRJOB command with assistant level *BASIC to show interactive jobs. This interface allows you to send a message to the user to sign off the session.
- The Best Web Links on Development: Tips, tutorials and more.
- Ask your programmer questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our programmer gurus are waiting to answer your technical questions.