Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


How to set Semaphore

Posted on 2009-04-15
Medium Priority
Last Modified: 2013-12-21
We have a ksh shell script. Sometimes, this ksh script is executed multiple times due to some error in the calling program which we do not have control over. Until that is fixed, we are planning to use semaphore type control so that it won't be executed twice.
I got a semaphore example from "http://www.csm.astate.edu/~rossa/semab.html" semabinit.c program.
How to have semaphore type control over ksh shell script?
Question by:srikanthradix
  • 3
LVL 62

Expert Comment

ID: 24155964
kill -0 `cat /var/run/myscript.pid || /opt/sbin/script.sh &\
 echo $! > /var/run/myscript.pid
LVL 62

Assisted Solution

gheist earned 800 total points
ID: 24155971
kill -0 `cat /var/run/myscript.pid` ||\
 /opt/sbin/script.sh &\
 echo $! > /var/run/myscript.pid
LVL 62

Expert Comment

ID: 24155982
Check if process with PID in file exists by sending it SIGNULL
|| - if return <> 0
Launch script in background
store new PID into file

LVL 68

Accepted Solution

woolmilkporc earned 1200 total points
ID: 24155987

Hi again srikanthradix,

I think using semaphores is not really necessary.
How about doing it this way:

- The script checks for existence of a 'lock' file.
- If there is a lock, complain about it (email?) and exit.
- If none exists, create one and start to execute.
- At end remove the lock and exit.
- Additionally, catch some signals (including the shell builtin 'ERR')
  to avoid leaving an orphaned lock.
It could look like below.

The script will remove the lock when terminated by INT, QUIT, TERM,
or if any command inside has a returncode > 0, or at its regular exit.
When killed by TERM, the trap handler will run twice. Don't worry about that,
it's obviously a blur in the handling of 'ERR'.
Sure you can add more signals and tailor the 'echo' messages (or send email instead).

Add the lines up to '#--- Main Body ...' at the top of your script,
and replace (or augment) its exit with the last part.
Try it!


server=$(uname -n)
script=$(basename $0)
 echo "\\nRemoving lock ${lock} and exiting ${script}."
  rm ${lock} 2>/dev/null
  exit $?
trap Cleanup 2 3 15 ERR
# --- Lock handling --- #
if [ -e ${lock} ]
   echo "$(date) While running ${script} on ${server}: Lock ${lock} exists; exiting."
   exit 99
   touch ${lock}
# --- Main Body --- #
# >>> Your regular processing here <<< #
# --- Exit gracefully (remove lock) --- #

Author Closing Comment

ID: 31570728
Thanks for solution. it is working perfectly fine.

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question