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

x
?
Solved

How to set Semaphore

Posted on 2009-04-15
5
Medium Priority
?
1,999 Views
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?
0
Comment
Question by:srikanthradix
  • 3
5 Comments
 
LVL 62

Expert Comment

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

Assisted Solution

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

Expert Comment

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

0
 
LVL 68

Accepted Solution

by:
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!
 
Cheers

wmp


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

Author Closing Comment

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

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