hi,
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 ...

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:

#!/bin/sh

touchfile=/tmp/running

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

### test run ###

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

-> ./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:

#!/bin/sh

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

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

### test run ###

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

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

I hope this helps!
-G

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, networking, learning, and sharing knowledge.