- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Looping a script with sleep..
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2001 07:38 AM
08-11-2001 07:38 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2001 07:45 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2001 08:04 AM
08-11-2001 08:04 AM
Re: Looping a script with sleep..
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2001 08:07 AM
08-11-2001 08:07 AM
Re: Looping a script with sleep..
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2001 08:35 AM
08-11-2001 08:35 AM
Re: Looping a script with sleep..
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2001 08:50 AM
08-11-2001 08:50 AM
Re: Looping a script with sleep..
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-12-2001 08:09 AM
08-12-2001 08:09 AM
Re: Looping a script with sleep..
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2001 08:52 AM
08-13-2001 08:52 AM
Re: Looping a script with sleep..
that sounds like some really good stuff.
If you dont mind taking the time.
How would I go about doing that?
Richard
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2001 08:57 AM
08-13-2001 08:57 AM
Re: Looping a script with sleep..
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2001 09:08 AM
08-13-2001 09:08 AM
Re: Looping a script with sleep..
'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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-13-2001 09:52 AM
08-13-2001 09:52 AM
Re: Looping a script with sleep..
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-14-2001 02:50 AM
08-14-2001 02:50 AM
Re: Looping a script with sleep..
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