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?
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.
#!/bin/bash
while true; do
# Do something
sleep 5;
done
You can create a my-task.sh file with the contents above and run it with sh my-task.sh. Optionally you can configure it with supervisor as a service so that it would start when the system boots, etc.
It really does sound like you're doing something that you probably shouldn't be doing though. This feels wrong.
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.
"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."
You could use a SystemD timer unit, which will trigger a service - that you'd set up to do what you want - every 5 seconds.
Suppose your service unit is called mystuff.service and is installed in /etc/systemd/system (check out SystemD user services if you want to replace a user's crontab), then you can write a timer unit to run the service at boot time and then every 5 seconds, like this:
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.
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.
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.
You can create a
my-task.sh
file with the contents above and run it withsh my-task.sh
. Optionally you can configure it withsupervisor
as a service so that it would start when the system boots, etc.It really does sound like you're doing something that you probably shouldn't be doing though. This feels wrong.
You could have a
cron
job kick-off a script every minute that starts 12 backgrounded processes thusly:dostuff.sh
:My question, though, is What on EARTH could you be doing that needs to run every 5 seconds?
Just use a loop:
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.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."
You could use a SystemD timer unit, which will trigger a service - that you'd set up to do what you want - every 5 seconds.
Suppose your service unit is called
mystuff.service
and is installed in/etc/systemd/system
(check out SystemD user services if you want to replace a user's crontab), then you can write a timer unit to run the service at boot time and then every 5 seconds, like this:/etc/systemd/system/mystuff.timer
Then reload the systemd configuration, enable the timer unit and start it.
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
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.
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.
The 'exec $0' restarts the script, but replacing the running script. It can be initially started with a crontab '@reboot' line.