how can i pervent from crontab concurrent run ???
thats mean when my crontab Take too long , with this crontab another crontab not runs...
please help me ...

Recommended Answers

All 4 Replies

Hi itengineer7!

This post is a little aged, so you've probably already figured this out by now, but it should be fairly simple to include logic that check to see if the script is already running, and exit if it is. That should PREvent concurrent runs.

Here's one method that uses a touchfile:



if [ -e $touchfile ]; then 
   echo already running
   echo starting
   touch $touchfile
   sleep 120
   rm $touchfile

### test run ###

-> ./rawr.sh &
[1] 21778

-> ./rawr.sh
already running

You could also do something using 'ps'. With the ps method it might be easier to write a "launcher" script to actually run from cron, to check and see if your script is running and not start a second copy.

If you are going to use a file-based approach why not use something that is somewhat more robust? lockfile already accomplishes what Gromit suggests but provide a more standard means to achieve it.

Thanks for the tip L7Sqr!

I researched 'lockfile', and it looks like it's a function of the 'procmail' package, and therefore probably not quite as universal/portable as simply touching a file. You still have to do the same test to see if the file exists, so I'm not sure I see the benefit.

'Lockfile' has some useful features, but I'm not sure it's worth installing 'procmail' in an environment where it won't be used just to get it.

Here's another example, using a "launch" script to see if your script is already running before trying to launch it again:


echo "checking for a running instance of some_script.sh..."

if ps -ef | grep -q [s]ome_script.sh; then
  echo "already running"
  echo "launching"
  sh some_script.sh &

### test run ###

-> sh launch.sh 
checking for a running instance of some_script.sh...

-> sh launch.sh 
checking for a running instance of some_script.sh...
already running

I hope this helps!

I agree that there is no need to overdue a solution. However, in the case that you'd like to block until one script finishes until you begin yours lockfile allows for more flexibility - and probably less of a race condition - then a homegrown implementation.

There is nothing directly wrong with the methods you provide, don't get me wrong. I was just offering another option that might fit.

Also, instead of ps ... | grep you may want to look into pgrep .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.