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?
Best Answer
Yes, it is possible. You should define two task job, here is an example:
First create
startTaskJob.conf
:You can test it with:
and the output will be saved in
/var/log/upstart/startTaskJob.log
Than create
stopTaskJob.conf
:This script will be executed every time system enter in
runlevel
0, 1 or 6. At shutdownrunlevel
become 0 and upstart init process will run it because of "start on runlevel [016]".You can test it:
UPDATE: This is an example on how to do this in a single file.
I tested it on lubuntu 12.04 and this is
/var/log/upstart/taskJob.log
content after restart: