Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a unix script to run every 15 seconds?

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.

like image 320
Nick Sergeant Avatar asked Jun 23 '09 18:06

Nick Sergeant


People also ask

How do I run a cron job every 10 seconds?

“cron job every 10 seconds” Code Answer's*/10 * * * * * will run every 10 sec.

How do I run a cron job every 15 seconds?

“run cron job every 15 seconds” Code Answer's*/10 * * * * * will run every 10 sec.

How do I run a script every second in Linux?

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.

How do I run a script every second?

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.


2 Answers

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

like image 58
rasjani Avatar answered Sep 22 '22 08:09

rasjani


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.

like image 35
RichieHindle Avatar answered Sep 26 '22 08:09

RichieHindle