Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2002
    Posts
    202

    [bash/linux] single script instance running only

    I have a bash script which runs in the crontab every 5 minutes.

    I want the script to only execute if a current instance of the script is not already running. If it's already running/processing, then to do nothing.

    Whats a good way to do this?

  2. #2
    Join Date
    Nov 2002
    Posts
    202
    I have an idea of using the following code:

    PHP Code:
    ps u grep script.sh awk '{print $11}' 
    But i have a further question for this approach:
    1. how do i grep a path name like "/usr/local/program.sh"

    Cheers!

  3. #3
    process=`ps auxw | grep script.sh | grep -v grep | awk '{print $11}'`

    if [ -z "$process" ]; then

    /path/to/your/script.sh

    fi



    Try this?

  4. #4
    Join Date
    May 2004
    Location
    Lansing, MI, USA
    Posts
    1,548
    Code:
    if [ -f /path/to/lockfile ]
    then
      echo Lock file found...
    else
      touch /path/to/lockfile
      ... code ...
      rm -f /path/to/lockfile
    fi
    Jacob - WebOnce Technologies - 30 Day 100% Satisfaction Guarantee - Over 5 Years Going Strong!
    Website Hosting, PHP4&5, RoR, MySQL 5.0, Reseller Hosting, Development, and Designs
    Powered By JAM - Professional Website Development - PHP, MySQL, JavaScript, AJAX - Projects Small & Large

  5. #5
    Join Date
    Nov 2002
    Posts
    202
    intelligent, thanks webonce

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •