The following Upstart script, saved as /etc/init/upstart-test.conf works:

start on filesystem
stop on on runlevel [!2345]

pre-start script
  set -x
  [ ! -f /etc/default/grub ] && { stop; exit 0; }
  . /etc/default/grub
  # [ ! -f /etc/default/grub ] && { stop; exit 0; }
end script

  set -x
  . /etc/default/grub
end script

Starting it (under Ubuntu 14.04.5):

$ sudo initctl start upstart-test
upstart-test start/running, process 2380

If I uncomment the second file existence test, the job fails:

$ sudo initctl start upstart-test
initctl: Job failed to start

Why? The log shows:

$ sudo cat /var/log/upstart/upstart-test.log 
+ [ ! -f /etc/default/grub ]
+ . /etc/default/grub
+ lsb_release -i -s
+ [ ! -f /etc/default/grub ]

The file never goes away. The second file test line is identical; I even tried both copy/pasting it and typing it out manually to be sure. What's going on?

  • Someone got this on Twitter, waiting to see if they'll post the answer here. Basically you need to end the stanza with something "truthy". – detly Oct 3 '16 at 11:19

I’ve never used upstart, so this may not be the whole story.

Try adding true to the end of your pre-start script, just like the other script in your file.

When /etc/default/grub exists, your [ command (which is the same as test) returns a non-zero exit status, which is a “falsy” value. This makes the whole command with the && and the rest of the line false, so as intended, the right hand side doesn’t run, but this is the last command in the pre-start script, and upstart may see a non-zero exit status in the last command as a failure.

  • This seems to be it. The irony is that I added the true command to the script stanza just to fill it out. It didn't occur to me to try adding it to the pre-start stanza. – detly Oct 3 '16 at 11:30
  • exit 0 will also do it, in fact. – detly Oct 3 '16 at 11:32

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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