Operating System - HP-UX
1755741 Members
4287 Online
108837 Solutions
New Discussion юеВ

Running One Instance of Script at a TIme via Cronjob

 
SOLVED
Go to solution
susee_sundar
Regular Advisor

Running One Instance of Script at a TIme via Cronjob

HI ,

I am coming again with one more query,

We are scheduling the cronjob to run a script once in five minutes.

But now the issue is:
Sometimes the script takes more than 5 minutes to complete and the other instance of the same script started from the cron.

We don't know how to overcome from this issue.,

We want once the script completed then it needd to wait for 5 minutes and start the script again.

So the Time interval between the script_complete & Script_start is 5 Minutes.

----------------------

Is there any way to achieve this task

Regards
Suseendran .A
11 REPLIES 11
sg222
Occasional Advisor

Re: Running One Instance of Script at a TIme via Cronjob

So why are you using cron?

You could insert at the end of the script a:
sleep 300
line when the script's job is done.

So they will always be just one instance.

More difficult is to stay with cron but insert a proof at the beginning of the script:

ps -eaf | grep your_script_name || start your script's job... not with that syntax, but this way.

greetings,

alexander meyer
sg222
Occasional Advisor

Re: Running One Instance of Script at a TIme via Cronjob

I've forgot to say:
my first solution has to be inserted in a while true loop.

script:

while true
do

your script's job

sleep 300

done
SANTOSH S. MHASKAR
Trusted Contributor
Solution

Re: Running One Instance of Script at a TIme via Cronjob

It is simple,

Stop the cron or for time being remove this
script from crontab,

Set a flag say 'export STATUS=0' indicating previous instance
of script has finished in some files say file1.
through ur script at the beginning

modify ur script as follows

##Check for this flag at the beginning

. file1
if [ $STATUS -eq 0 ]
then
## Now set STATUS=1 indicating script is running.
echo "export STATUS=1" > file1
< part of ur script>
## After finishing set STATUS=0
echo "export STATUS=0" > file1
else
exit
fi


-Santosh
SANTOSH S. MHASKAR
Trusted Contributor

Re: Running One Instance of Script at a TIme via Cronjob

Sorry,

Forget to tell to restart cron / add ur script in crontab.

-Santosh
Yogeeraj_1
Honored Contributor

Re: Running One Instance of Script at a TIme via Cronjob

hi,

you can achieve this using "at":

at -f /path_to_script/script.sh +5 minutes > /dev/null


regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
susee_sundar
Regular Advisor

Re: Running One Instance of Script at a TIme via Cronjob

HI Yogee & SG.,

Your points are true & acceptable But When The server is restarted then we need to start the task again..

But Santhosh is Right,
susee_sundar
Regular Advisor

Re: Running One Instance of Script at a TIme via Cronjob

But Santhosh,

I didn't fully understood the concept.,Where Do I insert the STATUS flag on my script and how will it work..

Regards
Suseendran ,A


Yogeeraj_1
Honored Contributor

Re: Running One Instance of Script at a TIme via Cronjob

hi Suseendran,

when the server is restarted, you will have a startup script that will start the at job.

create the script in /sbin/init.d/xxx

then configure it to start at the appropriate run level.

should not be something too complex!

hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
OldSchool
Honored Contributor

Re: Running One Instance of Script at a TIme via Cronjob

quote:
I didn't fully understood the concept.,Where Do I insert the STATUS flag on my script and how will it work

Ok, Let's go through Santosh's script. First off, in the example a file "file1" exists in the current directory. The contents of "file1" is either:

export STATUS=1
or

export STATUS=0

the contents will depend upon whether or not another copy of the script is already running.

1) ##Check for this flag at the beginning
2)
3) . file1
4) if [ $STATUS -eq 0 ]
5) then
6) ## Now set STATUS=1 indicating script is running.
7) echo "export STATUS=1" > file1
8) < part of ur script>
9) ## After finishing set STATUS=0
10) echo "export STATUS=0" > file1
11) else
12) exit

Line 3, "file1" is sourced, which makes the value of STATUS available to the rest of the script.

Line 4, check the current value of STATUS, if 0 continue executing the "then" block, begining in line 5

Line 7, update "file1" to indicate that a copy of the script is *now* running

Line 8, insert your commands/script here and it will run only one copy at a time

Line 10, update "file1" to reflect the job is complete and another job may run.

Line 11, null else *no action*

and finally, Line 12 exits.

==================================================

Now the issue is, do you really want the jobs to start every 5 minutes w/o running multiples concurrently? If so, "cron" and the Santosh's script using a lockfile will do this quite nicely.

If however; the goal is to have a 5 minute separation between the end of one job and the start of another, then insert the "at" command Yogeeraj noted at the end of your existing script. NOTE: this will cause the actual start times to vary, as it will be based on the end-time of the previous job, and as Yogeeraj also noted, you will need some kind of start-up/shutdown script to cope w/ reboots and shutdowns.

Hope this helps......