0

If i have the following entries in my cron table:

00 03 * * * /java_prog1.sh 
00 5 * * * /java_prog2.sh

The first job usually takes around 30 minutes to complete. The second job takes about 10 minutes. There are some exceptional situations where the first job takes more than two hours.

Is there a way i can configure the two jobs so that the second job does not start if the first one is still running?

I have seen several examples using tools like flock but i think flock does not apply here as i am not trying to prevent the same job running at the same time. I am trying to prevent another job to start if the previous one is still running.

3
  • 1
    Make your first cron job emit the PID somewhere on disk when it starts and clean it up when it's done. That would make way for second to determine the status of the first. Commented Mar 15, 2014 at 18:22
  • Will the PID be the same for the first job every time it runs? I thought the PID is different on every execution. Commented Mar 15, 2014 at 18:24
  • I mentioned emit the PID on disk -- it implied write it to a file with a known name. Commented Mar 15, 2014 at 18:25

3 Answers 3

1

Just putting some flesh on top of the suggestions already there in the comments.

Put in the beginning of java_prog1.sh:

[ -f /var/run/java_prog1.sh.pid ] && exit
echo "$$" > /var/run/java_prog1.sh.pid

... everything else our script does ...

rm -f /var/run/java_prog1.sh.pid

Then in the beginning of java_prog2.sh:

[ -f /var/run/java_prog1.sh.pid ] && exit

... the rest of your java_prog2.sh ...

Or:

if [ -f /var/run/java_prog1.sh.pid ]; then
   kill -0 `cat /var/run/java_prog1.sh.pid` > /dev/null 2>&1 && exit
fi


... the rest of your java_prog2.sh ...

The first one will just exit immediately, if the a file (possibly and probably containing a PID from the first script) exists. The second one will exit only if the file exists and a process with a PID in in that file is in the process table (kill -0 will return 0 in case it does).

As far as the actual scripting goes, you probably have to experiment a bit. This is just to give you a rought idea how it might go.

Sign up to request clarification or add additional context in comments.

Comments

0

Is there a way I can configure the two jobs so that the second job does not start if the first one is still running?

Since I can't comment, I will give you another example of what you could consider but of course @sami-laine solution is more elegant.

You can simply edit a config file like that:

At startup:

echo 0 > file.config

In your /java_prog1.sh:

echo 1 > file.config    # at the beginning of the script

do something ...      

echo 0 > file.config    # at the end of script 
                        #let the other script know that the job was finished

In your /java_prog2.sh:

while [ `cat file.config` -eq 1 ] ; do sleep 5; done   # Wait whatever want

Comments

0

The creation of directories is atomic, so you could add this in both scripts:

lock_dir=/tmp/name_of_lock_directory
if ! mkdir "$lock_dir"; then
    echo "Cannot run $0: lock directory $lock_dir exists" >&2
    exit 1
fi
cleanup () { rm -r "$lock_dir"; }
trap cleanup EXIT
# if you want, you can add the running pid to a file in the lock directory
echo $$ > "$lock_dir/pid"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.