Operating System - HP-UX
1834149 Members
2367 Online
110064 Solutions
New Discussion

Looping a script with sleep..

 
SOLVED
Go to solution
someone_4
Honored Contributor

Looping a script with sleep..

Hello ..
Is there a way I can "loop" a script to run every 2 or 5 minites with a sleep with out running it from cron? I guess am looking for some kind of test statement or a times loop. To where if I run
./script & it will run in the background and execute every 2 5 minutes or whenever I want it to. If there is a way is this a good idea or not? My it director says that sometimes scripts go crazy if we run them from cron. He says: if your script goes banana for some reason cron will keep on running it
you could end up many many many zombies or runaway..

Richard
11 REPLIES 11
Praveen Bezawada
Respected Contributor
Solution

Re: Looping a script with sleep..

Hi
you can have something like this in the script (a.sh)

while true ;do
do your stuff
sleep 300
done

The script will do your stuff, sleep for 5 mins and then do yout stuff.....

...BPK...
and you can run the script as ./a.sh &
James R. Ferguson
Acclaimed Contributor

Re: Looping a script with sleep..

Hi Richard:

Here's one. Call the script "my.sh". This will excute a loop 10-times before terminating, pausing 3-seconds each time.

#!/usr/bin/sh
typeset -i I=0
while (( I < 10 ))
do
date
let I=I+1
sleep 1
done
#_end

This can easily be modiied to allow passing the number of iterations and the number of seconds to sleep, as:

#!/usr/bin/sh
typeset -i I=0
while (( I < $1 ))
do
date
let I=I+1
sleep $2
done
#_end

Thus, the command: ./my.sh 10 5

...will loop 10-times, pausing 5-seconds each time.

Regards!

...JRF...
Wodisch
Honored Contributor

Re: Looping a script with sleep..

Hello Richard,

well, how about "at" - if your account is permitted to use
it? Write a script, execute it it form"at" and make it
restart itself with "at" again a few minutes later...

If that script goes "crazy" it will not restart itself any
longer.

HTH,
Wodisch
someone_4
Honored Contributor

Re: Looping a script with sleep..

Hello

Mr. James: would I run your script from cron or with the & ?

Wodisch: I did a man on the at command. I am root so I am sure I can set that up. Can you tell me more about setting up that freature?

If I run the script on a non stop loop. even though it is not doing much is there a chance that cpu will go up? Or is there any harm from doing it that way instead of from cron? And how valid is my bosses comment about scritps going crazy and making zombies and runaways?


Richard

James R. Ferguson
Acclaimed Contributor

Re: Looping a script with sleep..

Hi (again) Richard:

You could run the script I proposed via 'cron" or as a background job. If you start it in the background, do so with 'nohup' to divorce it from your shell.

When a process "sleeps" it is taken off the CPU run queue. There is very, very little overhead for this. In fact, if you are continually testing for a condition or an event to happen, then you WANT to impose a wait or a sleep. The technique is perfectly valid.

As regards the 'at' mechanism. Wodisch's suggestion is a nice one. Indeed, it is often used in situations where you cannot easily 'cron' because the 'crontab' syntax doesn't easily offer the schedule you want. Consider a cron job that you want to run, say, every 13 days.

To allow the 'at' command for a user, you must add the user to the /var/adm/cron/at.allow file. The concept and the syntax is the same as for the /var/adm/cron/cron.allow file. See the man pages for 'at' and 'cron'.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Looping a script with sleep..

Hi Richard,

The usual reason that scripts 'go crazy' when run from cron (which is really the better way) is that typically the prior invocation of your cron script has not finished before the next is fired off. You could have exactly the same problem with a looping script with sleeps. I prefer to do this sort of thing from cron because then you don't have to worry about starting it on reboot or having to write an rc script to do it. The better approach to this problem is to put some logic in to create a lock file when your process starts, do your stuff, and remove the lock file. If the next invocation fired off by cron sees this lock file it, it exits and does nothing. A variation on this is to let the process write its PID in the lockfile and the next process also checks via ps if the prior process is still running. This is a good additional check if the process is critical. This construct covers the case where the prior process died without removing the lockfile.

Clay
If it ain't broke, I can fix that.
someone_4
Honored Contributor

Re: Looping a script with sleep..

Hey mr clay
that sounds like some really good stuff.
If you dont mind taking the time.
How would I go about doing that?

Richard
someone_4
Honored Contributor

Re: Looping a script with sleep..

One more question.
All the options that were giving to me on this post work great.
But what is the difference from doing as Mr. James said with the typeset option or doing what Mr. Bezawada recommended. Or is it just personal perference.

Richard
James R. Ferguson
Acclaimed Contributor

Re: Looping a script with sleep..

Hi Richard:

'typeset' in the case I used for you, declares an integer variable. This allows the expression to be tested syntatically correctly.

The shell allows (in fact, creates) variables as they are encountered. This is obviously not true in many languages.

Using 'typeset' allows me to predeclare them; to establish them as integers to make arithmetic faster; mark them as readonly; cause data that is stored in them to be automatically converted from upper-to-lower case; etc.

Perhaps the most powerful use of 'typeset' is to declare variables as "local" to a function or procedure. In this way, the variable only exists (or its value only exists) within the scope of the function or procedure.

Take a look at the man pages for 'sh-posix' and look at 'typeset' there.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Looping a script with sleep..

Here you go Richard,

This is an example of a script whicj uses a lock file to store the PID. The next time it is run, if the lock file is found it then reads the file to get the previous pid and does a ps to look for that process. If the process is found, the old one is still running and this invocation simply exits.

Clay
If it ain't broke, I can fix that.
Magdi KAMAL
Respected Contributor

Re: Looping a script with sleep..

Hi Richard,

if you want to run your script ( example : doMyJob.sh ) with a different sleeping time ( expressed in seconds ) each time you execute it ( after stopping it of course ) :

while true
do

Commands ....

sleep $1
done

#doMyJob.sh 10
#doMyJob.sh 20
...


Magdi