Solved

How to ignore jobs already running in cron

Posted on 2015-01-30
5
139 Views
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.
0
Comment
Question by:naveenraj
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:Rob_Jeffrey
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.
0
 
LVL 61

Expert Comment

by:gheist
ID: 40580815
Run it every 2 minutes or get a faster system?
0
 
LVL 9

Accepted Solution

by:
Rob_Jeffrey earned 500 total points
ID: 40580877
An example script that can be modified to suit your needs:
#!/bin/sh
FILE=~/job_running.txt
if [ ! -f $FILE ]
then 
  touch $FILE
  echo "Running job"
  # Add command here
  rm $FILE
else 
  echo "Job already running"
fi

Open in new window

0
 
LVL 9

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).
0
 
LVL 61

Expert Comment

by:gheist
ID: 40581366
Just put it in a loop?
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
unix solaris snoop command 6 84
mobaxterm not able to change directory 28 103
AWS CLI - Issue with name display 2 50
how to rebuild XFS volume from LV 19 34
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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.
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.

911 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

25 Experts available now in Live!

Get 1:1 Help Now