#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    111
    Rep Power
    4

    Crontab help - prevent duplicate process


    Hi. I know how to set crontab to make a program run but is there a way or additional syntax/code I can add to the line of my cron job that whenever it runs the program it will check whether there's a process already running?
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,959
    Rep Power
    4035
    Have your cron job launch a shell script which checks if the process is running. If it is running, your shell script just exits without doing anything. If it is not running, your shell script would launch it.

    There are various ways to test if a process is running. A most common one is to record the PID when launching the process in a file somewhere. Your shell script can then test if that file exists, and if so check the the process specified by that pid is still running using the ps command.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    111
    Rep Power
    4
    That seems a bit hard for me as I don't do shell script. I just know how to set up the line on crontab.

    But I found this and it seems using flock. It doesn't explain much though. I'm wondering if it means when the script runs it creates a file which has a lock and will auto remove once it's done.

    So if the next job starts and see there's a lock then it won't lock again which means this next job will stop?

    Is this correct?

    http://blog.0x1fff.com/2011/01/cron-tip-prevent-running-multiple.html
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    111
    Rep Power
    4
    Hi. I just found this and it seems pretty good. My script is php.

    Here's the link for those one who's interested.

    http://abhinavsingh.com/blog/2009/12/how-to-use-locks-in-php-cron-jobs-to-avoid-cron-overlaps/

    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo