Make a script called foo that takes the arguments start, stop and restart. Put it in the directory /etc/init.d/
Here I'm assuming that your default runlevel is 2 (Debian and I guess Ubuntu default), so make a link:
ln -s /etc/init.d/foo /etc/rc2.d/S99foo
At boot time this script will be called with the "start" argument, at shutdown with the "stop" argument. Have a look at the other scripts in /etc/init.d/ and google for inspiration.