31

I am using Ubuntu 12.04 and want to have a service starting, when the system is booted normally.

As 'service' I understand some code, for example cd my_directory; my_command -host 0.0.0.0 -port 1234 -arg x that just should be running as if it has been started on the command line. There are services to be started as normal user, but also services to be started as root (in fact, it is not required the services to be run on user level).

I also require to configure the behavior when a 'service' stops. I want them to be restarted in my case, with the same argument, in a specified directory.

All of the services should be started automatically when the system is started normally, i.e. if the power switch is pressed. No other action should be required.

There are some documents spread on the internet, but they all confuse me. They talk about init, init.d, rc.d, but I never saw a simple-to-follow step-by-step instruction to easily as a service using e.g. upstart. If this is easy, I would appreciate if those steps are given here.

33

To create a job to be started automatically when Ubuntu starts, use the example given here. As written example, suppose to create the following file /etc/init/testservice.conf with sudo:

# testservice - test service job file

description "my service description"
author "Me <myself@i.com>"

# Stanzas
#
# Stanzas control when and how a process is started and stopped
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas

# When to start the service
start on runlevel [2345]

# When to stop the service
stop on runlevel [016]

# Automatically restart process if crashed
respawn

# Essentially lets upstart know the process will detach itself to the background
# This option does not seem to be of great importance, so it does not need to be set.
#expect fork

# Specify working directory
chdir /home/user/testcode

# Specify the process/command to start, e.g.
exec python mycommand.py arg1 arg2

To 'manually' start or stop the process use

sudo start testservice
sudo stop testservice

See the job control commands.

  • I needed "start on started mountall" to start the service. – martinedwards Mar 23 '16 at 10:09
23

Ok, Alex, the point is that all the userspace processes in Linux are started with init process, whose pid is 1. For instance, run pstree to see the tree of your processes, whose root is init.. There are several versions of init process implementation nowadays, most notable are

  • sysVinit (classical init, still used by some distributions, including older Debian)
  • Upstart init, used by older Ubuntu and some RHEL (Red Hat) and older Fedora versions
  • systemd init, used by modern Fedora, Ubuntu, Debian, RHEL, SUSE versions

Traditionally, Unix'es used init implementation called sysVinit init, called by the name of https://ru.wikipedia.org/wiki/UNIX_System_V version of Unix. It is very influential and other inits are backward compatible to it.

Basically, sysVinit first reads /etc/inittab file, decides, which runlevel to run and tells /etc/init.d/rc script to execute so-called init scripts. E.g. when it normally boots to a multi-user runlevel, which is usually runlevel 2 on Ubuntu, /etc/init.d/rc starts executing scripts in /etc/rc2.d. Files there are only symbolic links to scripts, while the scripts themselves are stored in /etc/init.d directory. The naming of those symlinks in /etc/rc*.d directories is as follows. Say, we've got the following scripts in /etc/rc2.d:

$ls /etc/rc2.d
S16rsyslog
S17apache2
K02network-manager

It means, that upon switching to runlevel 2 init process first kills network-manager processes, cause its script name starts with K - K02network-manager and then starts processes, whose names start with S. The two digits after S or K is the number from 00 to 99, which determines the order, the processes are started in. E.g. rsyslog is started before apache2, because 16 is less than 17 (that makes sense, cause you want apache to rely upon rsyslog's logging capacities, thus rsyslog should be started first). The scripts are casual shell scripts, executed by #!/bin/sh.

So, basically to start a program upon startup in sysVinit style, write your own script (copy-pasting it from any example, you've got in /etc/init.d), put it to /etc/init.d and create a symlink to it under a reasonable name, e.g. S99mytrojan in /etc/rc2.d. Here's an explanation of typical sysVinit scripts in /etc/init.d http://docs.oracle.com/cd/E19683-01/806-4073/6jd67r96g/index.html

Now, Ubuntu guys decided that they want additional functionality from init. They wanted a fast booting OS, so they wanted their scripts to be executed in parallel; they wanted dead processes to be automatically restarted; they wanted the processes to invoke each other in an explicit manner by events (so that apache is run by "syslog started" event, and syslog is run by "file systems mounted" event etc., so we have events instead of some numbers 00-99). Thus, they've made Upstart and here is how it works. Upstart initscripts are put in /etc/init directory (don't confuse with /etc/init.d). Upstart usually runs /etc/init.d/rc too, so it's gonna execute your sysVinit scripts normally. But if you want your script to be respawned upon exit - Upstart events are for you.

Although I can't check that my script is working, I suppose, that for your aims, you should write the following /etc/init/mytrojan.conf script:

start on runlevel [02]
respawn
exec mytrojan --argument X 

But if you need dependencies, at least filesystems and network, may be it makes sense to replace start on runlevel [02] with something like:

start on (local-filesystems and net-device-up IFACE!=lo)

WARNING: I didn't check the correctness of this, cause I can't. Especially, I'm not quite sure about how to start script after your network connection is up and running (I used this version). Try googling for "upstart on network up".

  • 1
    This is a very nice overview on the history of process started in Linux, but it does not answer by question. I still do not know how to 'create' an upstart process. I also just want to use it , not understand every tiny bit on how it works. I just wanted to have a simple and easy-to-use working example, of how to create a process to be started and respawned automatically with ubuntu's upstart..l. – Alex Jul 24 '13 at 15:44
  • @Alex: updated with a sample scipt. Note, that my script might very well be wrong. I did it in spirit of alexreisner.com/code/upstart. – Boris Burkov Jul 24 '13 at 16:13
  • 1
    Upvoted because this is one of the best answers I have ever read. – user1301428 Apr 13 '14 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.