How do I prevent multiple instances of same process to run at the same time?

Posted on 2004-11-28
Last Modified: 2013-12-26
    I would like to know the proper way to prevent the same instance of processes from running at the same time on UNIX. I would like to allow only one instance of process to run. If the same program were to execute again, it will check if there is same instance already running. If yes, then it will immediately terminate itself.

Best regards,
Question by:nisa
    LVL 48

    Accepted Solution

    There's a few ways of doing it, but a very common way is to create a run file with the pid of the process.

    A number of startup scripts for daemons use this method (varies slightly between Unix flavours).

    For example:


    prog=`basename $0`

    if [ -f /var/run/$prog ]
         pid=`cat /var/run/$prog
         if ps -p $pid >/dev/null
               echo "$0 already running"

    echo $$ >/var/run/$prog

    Author Comment

        I accept the answer, thanks!. I have another question, I will give u extra 50 pts for that (if u have the info):

    I know that we can use unix "system()" call ie system(<the script command here>) so that my C program can call it internally. Is there any alternative I can use ie by using unix call directly (without using the script) from the C program?

    Best regards,
    LVL 48

    Expert Comment

    Your additional question depends entirely on what the script is doing.

    There are a lot of commands that have C library equivalents, it all depends on what functions you are trying to do.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 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

    12 Experts available now in Live!

    Get 1:1 Help Now