Running commands on boot in 11.10
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?
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:
script
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 :-)
Sounds like you were on the right track. Some of the confusion might arise because Ubuntu uses Upstart instead of the traditional Linux init system, but still supports the old methods. So you may see outdated methods that still work.
I'm not an expert, and I'm also not sure where you're getting lost, but have you read the information here: http://upstart.ubuntu.com/cookbook/
In /etc, there are a lot of examples to look at. I think the actual scripts are in /etc/init, with links in various directories like rc0.d, rc1.d, on up. These depend on what runlevels you want your script to run in. I seem to recall that Ubuntu doesn't really use many run levels, so you may see a lot of duplication or unused links.
Also, keep in mind that the same scripts are used for starting and stopping.