Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script that needs to be run every five seconds. I know that cron can do tasks by the minute, but is there a way to run something every second?

share|improve this question
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
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
up vote 30 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

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

* * * * * ~/

(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
What on EARTH - statistics, garbage collection, file sync, online status, you name it. – ruX Jul 16 at 23:53
Referencing external API for near-realtime data. Running request every 5 sec is MUCH better than doing it on every pageview. – Sergey Kudriavtsev Jul 18 at 21:41

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

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
I recommend editing this answer to expand it with specific details about how to do this. (See also How do I write a good answer? for general advice about what sorts of answers are considered most valuable on Ask Ubuntu.) – David Foerster Feb 23 at 8:51

Minimum configuration in cron is minutes, you can't set it for 5 seconds. You could use Quartz which does allow seconds.

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

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

"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

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.



# 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
Why not use a while loop instead of repeatedly restarting the script? – David Z Aug 4 '10 at 19:56

Your Answer


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.