Question

Note: I did consider putting this question on the AskUbuntu site, but believe it is a scripting/programming question at its core, and thus deserves to be here on SO.

I'm trying to use upstart to run a script every time I turn my Ubuntu desktop on. Specifically, I want to ping my router every 30 seconds (for reasons outside the scope of this question).

I read the upstart tutorial and also read up on a few example articles from around the internet. I am planning on writing and saving the script to a file called /etc/init/heartbeat.conf, which I believe is the correct location and file extension for upstart scripts (please correct me if I'm wrong).

Here is the contents of that script (which I have hacked together from various tutorials/examples online):

start on started network-manager
script
    ping -i 30 192.168.1.1 | cat >> ~/heartbeat/ping.log
end script

When I start my machine, I don't get any GUI/gnome-related errors, and don't see anything in /var/log/syslog, but I also don't see any ping output in ~/heartbeat/ping.log. Where am I going astray? Thanks in advance!

Was it helpful?

Solution

The location of the upstart script /etc/init.d/heartbeat.conf is correct, but there are few other issues with the script:

  • Recommended way to start the script is:

    start on filesystem and net-device-up IFACE!=lo

    This will ignore the lo (loopback) interface, and will also ensure that filesystems are mounted before the script runs (since you are writing to a file)

  • You have given path as ~/heartbeat/ping.log. But the script will run as root user, not your user! So checking in your home folder won't work. Also generally, upstart scripts don't honor ~ as you would expect. So always use a full path

  • If a folder called heartbeat doesn't exist, then the ping command itself would throw an error, saying ~/heartbeat: no such file or directory. The directory must exist in order for the redirection to succeed

Here is the updated script:

start on filesystem and net-device-up IFACE!=lo
stop on runlevel [016]
script
  ping -i 30 192.168.1.1 >> /full/path/to/ping.log 2>&1
end script

Optional items:

  • Do you really need a | cat in the original ping command?

  • Check /var/log/upstart/heartbeat.log for the actual upstart logs. If the script section itself did not succeed, then the error messages would be present in this log file.

Edit: Typo, start better, redirect error also to log file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top