How to ignore jobs already running in cron

Posted on 2015-01-30
Medium Priority
Last Modified: 2015-02-02
Hi , Is there a way i can ignore the run of a scheduled cron job if the jobs is already running on the host.

I am asking this because I have a cron entry to run a job every minute. Sometimes the script completes within a minute, some time goes more than a minute. When it runs more that a minute the next scheduled run tries to execute the script and this results in an cron error.
Question by:naveenraj
  • 2
  • 2

Expert Comment

ID: 40579841
You can write a wrapper script around the job which checks the existence of a file then if it doesn't exist, make the file, run your command, then delete the file.
LVL 62

Expert Comment

ID: 40580815
Run it every 2 minutes or get a faster system?

Accepted Solution

Rob_Jeffrey earned 2000 total points
ID: 40580877
An example script that can be modified to suit your needs:
if [ ! -f $FILE ]
  touch $FILE
  echo "Running job"
  # Add command here
  rm $FILE
  echo "Job already running"

Open in new window

LVL 10

Expert Comment

by:Carlos Ijalba
ID: 40581227
As Rob Jeffrey has recommended, whenever we write a script that can be run in parallel (ie: multiple runs of the same script), we should always check if our script is already running and exit the script.

This is done either by using a flag file, or checking the process list with a ps command.

Note that if you use a flag file, you should also intercept interruption signals (ie: TRAPS) so if the script gets killed by a CTRL-C, or a kill command, it ends up in a controlled manner and deletes the flag file, otherwise if the script fails, you will have a permanent flag and next runs of the script will always fail until the flag file gets removed manually. (And of course document the flag file location and use for operators or other sysadmins).
LVL 62

Expert Comment

ID: 40581366
Just put it in a loop?

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
This installment of Make It Better gives Media Temple customers the latest news, plugins, and tutorials to make their Grid shared hosting experience that much smoother.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

587 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