Operating System - HP-UX
1834288 Members
1805 Online
110066 Solutions
New Discussion

How to prevent multiple sessions of a cron job

 
SOLVED
Go to solution
Dhandu Subramanian
Occasional Contributor

How to prevent multiple sessions of a cron job

I have to a cron job for every 5 minutes. I need to make sure that I do not run another process if the previous one has not finished running (ie., run only one instance at any time)
Help is appreciated.
14 REPLIES 14
Jordan Bean
Honored Contributor
Solution

Re: How to prevent multiple sessions of a cron job

Possibly the easiest method is to use a simple lock file.

At the start of the job, test for the existence of the file (say, /var/run/cron.whatever). If it exists, exit. If not, create it, perform primary task, and remove it.
Victor_5
Trusted Contributor

Re: How to prevent multiple sessions of a cron job

Dhandu Subramanian
Occasional Contributor

Re: How to prevent multiple sessions of a cron job

Hi Shawn,
Not able to get to doc.id A5456417. :-(
May be I do not have access to it. Could you copy paste here.. thanks, appreciate it.
A. Clay Stephenson
Acclaimed Contributor

Re: How to prevent multiple sessions of a cron job

Hi:

Jordan's answer is almost perfect and it is the standard idiom. However, if this is a critical process, I would do one more thing.
When you create the lock file write the PID of the process as the contents of the lock file.
When you try to start the job, it the file is there and a ps -e finds that PID then exit otherwise start the process. This prevents a critical process from running when the last invocation died for some reason and did not remove the lock file.

My 2 cents, Clay
If it ain't broke, I can fix that.
Dhandu Subramanian
Occasional Contributor

Re: How to prevent multiple sessions of a cron job

Hi Clay,
Do you have a sample code for reference.. Thanks
Victor_5
Trusted Contributor

Re: How to prevent multiple sessions of a cron job

Hi Dhandu:

Here is the original document(id:A5456417):

Problem Description

I have about 40 jobs on two different systems that are started by cron. Many of the jobs start at similar times. I want to know which
job has finished without having to go to the cronlog on each system.

How do I set up cron to identify jobs when it sends me mail?

Configuration Info

Operating System - HP-UX
Version - 10.20
Hardware System - HP9000
Series - K460

Solution

When cron sends mail, it is generally mailing the stdout and stderr of the job that has executed. If you want specific information, the best way is to redirect your stdout and stderr into a file that has a header containing the job name. After the job is complete, make the script that runs the job e-mail the file.

Example cron entry:

* * * * * /usr/local/bin/myscript.sh > /tmp/myscript.out 2>&1

Example lines in myscript.sh:

#!/usr/bin/sh
echo "this is myscript.sh running at `date`"
mailx -s myscript.sh root < /tmp/myscript.out
rm /tmp/myscript.out

A. Clay Stephenson
Acclaimed Contributor

Re: How to prevent multiple sessions of a cron job

Hi again,

You were lucky, I had something very close and spent about 2 minutes altering it. Since this will be a cron job make sure that you have PATH set early in your script if you are calling anything not in /usr/bin.

Regards, Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to prevent multiple sessions of a cron job

Ooops, I think much faster than I type. Here is a correct version of the locked function.

Sorry about that, Clay
If it ain't broke, I can fix that.
Jordan Bean
Honored Contributor

Re: How to prevent multiple sessions of a cron job

I use ps -p $pid to verify the existence of processes not owned by the invoking process, and kill -0 $pid to verify processes owned by the invoking process.
Thierry Poels_1
Honored Contributor

Re: How to prevent multiple sessions of a cron job

Hey, how about some mega bonus points for Clay ? I really liked his idea about putting the process ID in the lock file.
great tip, tnx Clay.
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Dhandu Subramanian
Occasional Contributor

Re: How to prevent multiple sessions of a cron job

Hi Clay
I created a script file called run.sh from your sample code with process I want to run next your comment line 'put your processing here'. When ran the script this is the error i am getting
run.sh: function: not found
rub.sh: cannot return when not in function

What wrong am I doing?..
your help is appreciated..
A. Clay Stephenson
Acclaimed Contributor

Re: How to prevent multiple sessions of a cron job

Hi,

Sorry, I've been coding in too many languages today. I should have left out the word function

decclare the function simply as
locked()
{
....
....
return ${LSTAT}
}

That will fix you. My mistake not yours.

Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to prevent multiple sessions of a cron job

Hi again Dhandu:

Actually the function locked syntax does work in POSIX shell but locked() works in bourne,POSIX, and Korn shell.

Regards, Clay
If it ain't broke, I can fix that.
Ralf Hildebrandt
Valued Contributor

Re: How to prevent multiple sessions of a cron job

Don't use cron, use at.
Have the job reschedule itself AFTER it finished it's work.
Postfix/BIND/Security/IDS/Scanner, you name it...