Run Commands on Boot – Ubuntu 11.10 Guide


So I am getting properly confused on how to create a startup script on my Ubuntu 11.10 server. I have read about init-scripts, upstart-jobs and more, but I get even more confused the more I read.

I've tried various guides, but I just haven't found anyone that actually work.

Can someone show me how I create a simple script that will work in 11.10?

Best Answer

Marty Fried's answer contains the one most valuable info: the Cookbook. Reading thru that makes you more than able to write your init scripts.

However, messing with init.d, rc*.d, chkconfig et al, is not what you want to do. On Ubuntu (and other distros), they're just remains of the old sysvinit stuff which many packages still use or just support for legacy reasons. YOU DON'T NEED OR WANT TO GO THERE :-)

Simplest of all Upstart scripts is starting a daemon (put it in /etc/init/mydaemon.conf):

exec /path/to/binary

Thats ALL you need. That makes Upstart run the daemon when you do start mydaemon.

OK, you want it to start automatically? Usually, starting after dbus is a logical choise, so lets do that:

start on started dbus
stop on stopping dbus
exec /path/to/binary

This simple script starts your daemon whenever dbus has started and will stop it just before dbus stops.

You want it to respawn (re-launch) if it crashes? No problem, just add respawn on its own line to the file.

Your daemon forks or daemonizes itself? Well lets catch it nonetheless! Add expect fork in case of single fork, or expect daemon in case of true (double-fork) daemonization.

Lets summarize a simple startup script for your daemon:

author "Your name goes here - optional"
description "What your daemon does shortly - optional"

start on started dbus
stop on stopping dbus

# console output  # if you want daemon to spit its output to console... ick
respawn # it will respawn if crashed/killed

exec /path/to/binary

In case you don't wanna run a daemon, but just a series of commands, lets forget the exec line and add a script section:

   echo "Hello world!"
end script

This makes Upstart run the script instead of the daemon. The script section is just a normal shell script, so you can do pretty much anything you want inside of it.

Hope it helps. Stick to the Upstart configuration files and don't mess with legacy sysv and you'll keep sane :-)

Related Question