I know how to do every minute, but how about every 10 seconds?
6 Answers
I had a similar task last week. My solutions was to multiply the standard cron entries to the desired frequency. My crontab looks like:
* * * * * /usr/local/bin/php /var/www/myscript.php
* * * * * sleep 10; /usr/local/bin/php /var/www/myscript.php
* * * * * sleep 20; /usr/local/bin/php /var/www/myscript.php
* * * * * sleep 30; /usr/local/bin/php /var/www/myscript.php
* * * * * sleep 40; /usr/local/bin/php /var/www/myscript.php
* * * * * sleep 50; /usr/local/bin/php /var/www/myscript.php
If you want to check results of myscript.php, e.g. for debugging, just append
&> /tmp/myscipt.log
to each line in the crontab above. Then stderr and stdout get redirected to the log file.
- 476
- 1
- 4
- 10
-
2+1 for creative cron use – shodanshok Oct 26 '17 at 21:20
-
2This is the best solution IMHO. It's clear what it's doing and does not require a script or third-party solution. – Jeremy Jul 19 '18 at 15:25
You can't schedule the job every ten seconds, but I suppose you could schedule the job to run every minute, and sleep in a loop in 10s intervals. This would be predicated on your command being completed before the ten second interval expires, or you'll get overlap when the next command runs. This feels like a precarious solution, but if you can guarantee very short execution of the main command of the script, it would work.
#!/bin/bash
i=0
while [ $i -lt 6 ]; do
/run/your/command &
sleep 10
i=$(( i + 1 ))
done
- 1,135
- 1
- 10
- 16
-
I do a similar thing... I just use *'s in crontab, and then my script uses flock and loops forever, never quits unless it can't obtain a lock. – Peter Mar 05 '16 at 12:53
Cron only allows for a minimum of one minute. You can try this -
* * * * * ( sleep 10 ; /usr/bin/wget http://api.us/application/)
- 141
- 4
I'd use Monit and set the cycle time to 10 seconds. This is a clean way to manage this outside of the cron system.
I do this with certain scripts that need to run on a 15-second interval.
See: How to perform incremental / continuous backups of zfs pool?
- 194,921
- 91
- 434
- 799
If you want to go sub 10 seconds, e.g. 5 second, I recommend to make a worker loop with a little script like that:
#!/bin/bash
INTERVAL=5
while true; do
echo "do something"
# wait for next interval
WAIT_UNTIL=$(($(date +%s) + $INTERVAL))
while [ $(date +%s) -lt $WAIT_UNTIL ]; do
sleep 1
done
done
If you need to go sub second, add microseconds to the date command.
- 476
- 1
- 4
- 10
* * * * * script to run
* * * * * sleep 10; script to run
* * * * * sleep 20; script to run
Here script can be run with the intervel of 10 seconds...
- 1,944
- 2
- 8
- 17
-
This solution has already been posted 5 years ago. You should just upvote it instead of posting it again. – Gerald Schneider May 28 '19 at 13:03