Blog tvlooy

Releasing File Member Locks With QSH

IBM i | April 22, 2011

This article was published at IT Jungle.

Sometimes our users just keep their sessions open when they go home. At times, such as when we need to install software updates, this is a problem.

It is possible to write a CL that cleans up the file/member locks without getting too complicated? You'll be happy to know it is. Unix to the rescue! All you need is to quickly hack together a small nine-line QSH shell script to do the job.

See this example file named unlock that lives in my /home/tvl directory on the IFS:

for file in $@; do system "WRKOBJLCK OBJ($file) OBJTYPE(*FILE) MBR(*ALL)" 2>/dev/null | grep HELD | cut -c 16-46 > /tmp/.jobs.tmp while read lock; do if [ `echo $lock | wc -c` -gt 1 ]; then system "ENDJOB JOB(`echo $lock | cut -d " " -f 3`/`\ echo $lock | cut -d " " -f 2`/`\ echo $lock | cut -d " " -f 1`) OPTION(*IMMED) LOGLMT(0)" 2>&1 | \ grep -v CPC2206 | grep -v CPC1230 fi done < /tmp/.jobs.tmp rm /tmp/.jobs.tmp done

This script can be called from IBM i with:

QSH CMD('sh /home/tvl/unlock FILE1 FILE2 FILE3 FILEn')

Or, you can feed it the result of a file list:

QSH CMD('sh /home/tvl/unlock `cat /home/tvl/files`')

Or, even feed the result of a DB2/400 SQL query. For example:

QSH CMD('sh /home/tvl/unlock `db2 "select name from allfiles where changed=1"`')

The script will, for every file, first collect jobs that have a file/member lock on the object and output the job details into a temporary file. Next, the script will *IMMED end the job that is holding the lock. The two messages that I am ignoring are:

CPC2206 - Ownership of object QZSHSYSTEM in QTEMP type *USRSPC changed CPC1230 - LOGLMT changed for job

Using QSH makes quick system administration jobs easy. No compiling needed, just use the power of a Unix shell.