How to set Semaphore

Posted on 2009-04-15
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 "" semabinit.c program.
How to have semaphore type control over ksh shell script?
Question by:srikanthradix
    LVL 61

    Expert Comment

    kill -0 `cat /var/run/ || /opt/sbin/ &\
     echo $! > /var/run/
    LVL 61

    Assisted Solution

    kill -0 `cat /var/run/` ||\
     /opt/sbin/ &\
     echo $! > /var/run/
    LVL 61

    Expert Comment

    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


    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

    Thanks for solution. it is working perfectly fine.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    FreeBSD on EC2 FreeBSD ( 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…
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now