4

I would like to have foo do-startup-things run on boot, and foo do-shutdown-things run on shutdown where foo is my own program.

It looks like Upstart is a good candidate to do this, but Upstart seems to be designed for use with daemons, so running service foo stop gives the error stop: Unknown instance: because the process executed when the startup job was run is no longer running.

Is there a way to use Upstart to execute tasks on startup and shutdown without starting a daemon?

4
  • You do know there is a skeleton daemon? /etc/init.d/skeleton should be pretty straightforward and have you set up your own daemon. And if that is too difficult we also have start-stop-daemon. serverfault.com/a/135882/229730 has an example for a very simple wrapper.
    – Rinzwind
    Sep 4, 2014 at 21:58
  • I don't want to set up a daemon. As per the question, I want to use Upstart to run commands on startup and shutdown that do create a persistent process. Sep 4, 2014 at 22:09
  • @DanielGibbs if you're not running a daemon, why are you doing service foo stop?
    – muru
    Sep 4, 2014 at 22:17
  • To simulate what happens when the system shuts down, so I don't actually have to shutdown the system every time I want to test the Upstart config. Sep 4, 2014 at 22:20

1 Answer 1

6

Yes, it is possible. You should define two task job, here is an example:

First create startTaskJob.conf:

# startTaskJob - 
#
# This service print "script start" and end 
description "print script start"
start on runlevel [2345]

task
console log
script
  exec  echo "script start"
end script

You can test it with:

sudo start startTaskJob

and the output will be saved in /var/log/upstart/startTaskJob.log

Than create stopTaskJob.conf:

# stopTaskJob - 
#
# This service print "script stop" and end 
description "print script stop"
start on runlevel [016]

task
console log
script
  exec  echo "script stop"
end script

This script will be executed every time system enter in runlevel 0, 1 or 6. At shutdown runlevel become 0 and upstart init process will run it because of "start on runlevel [016]".

You can test it:

sudo start stopTaskJob

UPDATE: This is an example on how to do this in a single file.

# taskJob - 
#
# This service print environment variable 
# start on runlevel 
description "print environment variable"
start on runlevel [0123456]
task
console log
script
if [ "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" -o "$RUNLEVEL" = "6" ]; then
    exec  echo "(stopTask) $UPSTART_EVENTS - $RUNLEVEL - job $UPSTART_JOB" 
else
    exec  echo "(startTask) $UPSTART_EVENTS - $RUNLEVEL - job $UPSTART_JOB"
fi
end script

I tested it on lubuntu 12.04 and this is /var/log/upstart/taskJob.log content after restart:

(stopTask) runlevel - 6 - job taskJob
(startTask) runlevel - 2 - job taskJob
5
  • Excellent, this looks like what I'm looking for. Is there no way to combine them into one file? Sep 4, 2014 at 22:50
  • I don't think, but not sure, I tried but without success, if I'll find out how to do it, I will post.
    – Lety
    Sep 4, 2014 at 22:59
  • 1
    @DanielGibbs finally I found how to write single task, it is simple now that I know :) There are upstart environment variables that can be used to know which event or "level of run" is going on.
    – Lety
    Sep 5, 2014 at 19:08
  • Cool, that's much nicer than using two files :) Should the above config also contain task? Sep 5, 2014 at 19:10
  • sorry, yes, I forgot it.
    – Lety
    Sep 5, 2014 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .