I'd like to run a command at startup which pings a certain address every 10 minutes and writes the result to a file. I've figured out now how to do the pinging and file writing and the 10 min intervals:

while true; do my-command-here; sleep 600; done

My question is, can I put this in /etc/init.d/rc.local or should I be putting it in /etc/rc.local or somewhere else entirely? I'm specifically concerned because it's an infinite loop so I'm not sure if I could put it in one of these startup scripts.

Some help would be appreciated. I'm using Ubuntu 12.04.5


This isn't really an infinite loop; it's a task that needs to run every ten minutes. As such the task can go into the task scheduler, cron.

Run the command crontab -e and add this single line to the bottom of the file:

*/10 * * * * /path/to/my-command-here

Ensure that my-command-here is an executable script (chmod u+x my-command-here) and that its first line starts with #! and the name of the script interpreter (typically #!/bin/bash).

Each entry in the pattern */10 * * * * maps to the minute(0-59), hour(0-23), day(1-31), month(1-12), and day of week(0-6, with 0=Sunday).

  • Thanks. That looks like a much better idea. Just one question; Once I've added the line using crontab -e, do I need to restart the cron service or something? How can I check whether it is attempting to execute my script and perhaps fail? – Dewald Swanepoel Mar 21 '15 at 20:14
  • 1
    The crontab command notifies the cron daemon automatically, so you don't need to worry about it. You can confirm it's working either by looking at the syslog (something like grep -i cron /var/log/syslog) or by modifying your program to write something to stdout and then capturing that output something like this, /path/to/my-command-here >/tmp/mch.log 2>&1. If you don't capture the output it will be emailed to your local account. – roaima Mar 21 '15 at 23:05
  • +1 for answering the actual spirit of the question rather than the wording. – Vality Mar 22 '15 at 1:20

can I put this in /etc/init.d/rc.local or should I be putting it in /etc/rc.local or somewhere else entirely?

If you put it into rc.local, you should put it in a subshell and fork that into the background. The last line of rc.local should be exit 0, and it not block, i.e., any foreground activities should be brief.

(while true
    do my-command-here
    sleep 600
done) &

The ( ) puts it in a subshell and the & puts it in the background.

If you have a problem, check this Q&A about debugging rc.local.

  • BTW, you may want to include echo $BASHPID > /tmp/myloop.pid inside the subshell before the loop so you can check on that process later. Or you could put it in a script somewhere, launch myloop.sh & from rc.local, and then you can find it by name. – goldilocks Mar 21 '15 at 19:26
  • Great answer. I'm still not sure though which of these rc.local scripts I should be using? Why would I choose /etc/init.d/rc.local over /etc/rc.local or vice versa? – Dewald Swanepoel Mar 21 '15 at 19:29
  • 1
    Use /etc/rc.local. /etc/init.d/rc.local is the SysV init service that actually runs the script at boot. – goldilocks Mar 21 '15 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.