I've seen a few solutions, including watch and simply running a looping (and sleeping) script in the background, but nothing has been ideal.
I have a script that needs to run every 15 seconds, and since cron won't support seconds, I'm left to figuring out something else.
What's the most robust and efficient way to run a script every 15 seconds on unix? The script needs to also run after a reboot.
“cron job every 10 seconds” Code Answer's*/10 * * * * * will run every 10 sec.
“run cron job every 15 seconds” Code Answer's*/10 * * * * * will run every 10 sec.
Use watch Command Watch is a Linux command that allows you to execute a command or program periodically and also shows you output on the screen. This means that you will be able to see the program output in time. By default watch re-runs the command/program every 2 seconds.
Based on the information you have given, the best answer is likely the simplest one. Use a shell script with a infinite loop and a 1 second delay. Have the script run outside a terminal. That way it runs in the background.
If you insist of running your script from cron:
* * * * * /foo/bar/your_script * * * * * sleep 15; /foo/bar/your_script * * * * * sleep 30; /foo/bar/your_script * * * * * sleep 45; /foo/bar/your_script
and replace your script name&path to /foo/bar/your_script
I would use cron to run a script every minute, and make that script run your script four times with a 15-second sleep between runs.
(That assumes your script is quick to run - you could adjust the sleep times if not.)
That way, you get all the benefits of cron
as well as your 15 second run period.
Edit: See also @bmb's comment below.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With