I have a script that I would like to run as a service on my Ubuntu Server 11.04 headless box. That is, I want to have the correct files under /etc/init.d/ and to be able to control it via the service command.

Is there any guide available that will show me how to do this?


Solution 1:

Unless you are using 15.04 or any later release (which uses systemd), you will probably be better off if you turn your script into an Upstart job, rather than follow the examples you'll find in /etc/init.d/ (which are System V-style init scripts, which Ubuntu and other distributions are moving away from).

The Upstart Cookbook has an enormous amount of information about how Upstart jobs work. Although I wouldn't describe it as a HOWTO, if you combine the information there with looking through the stock Ubuntu Upstart jobs in /etc/init/ you will start to get the picture.

I suggest starting with section 4 and 5 of the Cookbook, "Concepts and Terminology" and "Configuration"

Solution 2:

Here is a very simple example how to create an upstart script: https://wiki.frugalware.org/index.php/Upstart_Job_HOWTO

UPDATE: above link is broken. Here is an example of a script:

description "My startup service"
author "You or someone else"
version "3.14"

pre-start script
    echo 'Starting my service ...'
end script

start on net-device-up
stop on runlevel [!2345]

console output

respawn

exec /path/to/your/custom/script

Adapt as necessary.

You should place the script in /etc/init/yourfilename.conf

Then reload the configuration with:

sudo initctl reload-configuration

And if all is OK, you should be able to start it with:

sudo start yourfilename