Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to know how to run a script every 5 second. I know that cron can do every minute. How do you get intervals below that?

share|improve this question
3  
What problem are you trying to solve? Are you sure cron is the right solution? –  andol Aug 4 '10 at 19:11
    
I know cron isn't the right solution. I was saying it wasn't in the question. Please read. I am trying to make a script run every 5 seconds. –  myusuf3 Aug 4 '10 at 19:20
2  
Sorry, guess I got the question slightly wrong. Yet, still wondering if you are trying to solve the right problem. –  andol Aug 4 '10 at 19:25

7 Answers 7

up vote 22 down vote accepted

Cron only allows for a minimum of one minute. What you could do is write a shell script with an infinite loop that runs your task, and then sleeps for 5 seconds. That way your task would be run more or less every 5 seconds, depending on how long the task itself takes.

It really does sound like you're doing something that you probably shouldn't be doing though. This feels wrong.

share|improve this answer

Just use a loop:

while true ; do ./your-script & ; sleep 5; done

This will start your-script as a background job, sleep for 5 seconds, then loop again. You can use Ctrl-C to abort it, or use any other condition instead of true, e.g. ! test -f /tmp/stop-my-script to only loop while the file /tmp/stop-my-script does not exist.

share|improve this answer

You could have a cron job kick-off a script every minute that starts 12 backgrounded processes thusly:

* * * * * ~/dostuff.sh

dostuff.sh:

(sleep 5 && /path/to/task) &
(sleep 10 && /path/to/task) &
(sleep 15 && /path/to/task) &
(sleep 20 && /path/to/task) &
(sleep 25 && /path/to/task) &
(sleep 30 && /path/to/task) &
(sleep 35 && /path/to/task) &
(sleep 40 && /path/to/task) &
(sleep 45 && /path/to/task) &
(sleep 50 && /path/to/task) &
(sleep 55 && /path/to/task) &
(sleep 60 && /path/to/task) &

My question, though, is What on EARTH could you be doing that needs to run every 5 seconds?

share|improve this answer
    
Well, this came in handy to me when I made a script that repeatedly checks the /media directory for a drive I plug in to usb for automatic backups... –  VF1 Aug 22 '13 at 2:52

Use cacti to monitor router and switch,but Cron only allows for a minimum of one minute,so if one port/device down,there is no warning until two minutes past.

share|improve this answer

Minimum configuration in cron is minutes, you can't set it for 5 seconds. You could use Quartz which does allow seconds. http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html

share|improve this answer
    
It seems Quartz is not open source. Is this correct? –  txwikinger Aug 4 '10 at 19:24
    
It has an open source version. It doesn't have a page but just go to the download page. You don't have to fill out the form just click take me to the download. –  Cody Harlow Aug 4 '10 at 19:27

I've done this sort of thing very successfully (and the end result rans weeks at a time, till the machine is rebooted). As for what I was doing right now, updating information and putting it into cache - updating every 10 seconds.

#!/bin/sh

SLEEP=5

# do stuff
sleep $SLEEP

# do stuff
sleep $SLEEP

# do stuff
sleep $SLEEP

# do stuff
sleep $SLEEP

# echo and restart...
exec $0

The 'exec $0' restarts the script, but replacing the running script. It can be initially started with a crontab '@reboot' line.

share|improve this answer
4  
Why not use a while loop instead of repeatedly restarting the script? –  David Z Aug 4 '10 at 19:56

You could use the GNU package mcron, a "Vixie cron" alternative.

http://www.gnu.org/software/mcron/manual/mcron.html#Top

"Can easily allow for finer time-points to be specified, i.e. seconds. In principle this could be extended to microseconds, but this is not implemented."

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.