How to prevent job1 from running while job2 is running..

Hi,

Please I need your expert advise on how to prevent/lock from execution job1 while job2 is still running in Unix... THanks:)

Use lockfiles. Before any job starts, make sure it checks for a lockfile. If the file exists, exit. If it does not, have it create the lockfile and proceed to do its actual job. At the end, remove the lockfile.

If you have two jobs and one lockfile, you'll get only one job running at any given time.

Hi, kindly give an example script of lockfiles usage?

# cat one.sh
#!/bin/ksh

if [ -f /tmp/lockfile ]; then
        exit
fi
touch /tmp/lockfile
echo "Process one doing work..."
sleep 10
echo "Process one completed work..."
rm /tmp/lockfile
# cat two.sh
#!/bin/ksh

if [ -f /tmp/lockfile ]; then
        exit
fi
touch /tmp/lockfile
echo "Process two doing work..."
sleep 10
echo "Process two completed work..."
rm /tmp/lockfile
# ./one.sh &
[1] 7377
# Process one doing work...

# ./two.sh
# ./two.sh
Process one completed work...
# ./two.sh
Process two doing work...
Process two completed work...
# 

As you can see, process two, didn't start as long as process one was running.