Ubuntu – Ubuntu 15.04 systemd vs upstart

15.04bootservicessystemdupstart

Unlike the prior versions Ubuntu 15.04 now uses systemd instead of upstart. To start programs at boot I always created conf-files in /etc/init/. They would look like this:

start on login-session-start
script
/usr/bin/x11vnc -auth /var/run/lightdm/root/:0  -display :0  -rfbauth /etc/x11vnc.pass  -rfbport 5900  -noxrecord -noxfixes -noxdamage -forever -bg  -o /var/log/x11vnc.log
end script

Unfortunately, this does not work anymore in Ubuntu 15.04 and I was trying to find an equivalent to this conf-file that runs with systemd. Though the internet wasn't yet quite so resourceful.

What is necessary to start the above conf-file at boot? Are there translators between upstart and systemd?

Best Answer

If you want to, you can instead make a systemd .service file that does the same thing. A basic systemd .service file for the above command might look like the following:

[Unit]
Description=Start VNC server
Requires=lightdm.service
After=lightdm.service

[Service]
ExecStart=/usr/bin/x11vnc -auth /var/run/lightdm/root/:0  -display :0  -rfbauth /etc/x11vnc.pass  -rfbport 5900  -noxrecord -noxfixes -noxdamage -forever

[Install]
WantedBy=graphical.target

Things to note:

  • Because this service relies on lightdm starting up, I've added a Requires=lightdm.service. This means that if this service is requested to start, then lightdm.service will also be asked to start, but if lightdm.service fails to start, then this service won't be asked to start. Additionally, After=lightdm.service is needed so that this service will start after `lightdm.service has finished starting.
  • The command being run doesn't have -bg specified. In systemd, it's preferred that services stay in the foreground, so that systemd knows that they are running. Note that it is possible to specify a PID file for a service that goes into the background.
  • WantedBy=graphical.target specifies that, as part of starting up your GUI, this should also get started up.

To install this file so that the service is run as root, add this file into /etc/systemd/system. Then, run sudo systemctl enable <filename> (where <filename> is the name of the service file in /etc/systemd/system). A symlink should be created in /etc/systemd/system/graphical.target.wants/. After rebooting, when you run systemctl status <filename>, you should see that the service is recognized and not yet started.

Note that /etc/systemd/system/ is meant for system-wide services that the system administrator (you) add. /lib/systemd/system/ is meant for system-wide services installed by packages. You do not need to make a symlink to /lib/systemd/system/ for the service to load. The below table (taken from the systemd.unit manpage) describes what each location is for with regards to system services (there are separate paths for user services):

   ┌────────────────────┬─────────────────────────────┐
   │Path                │ Description                 │
   ├────────────────────┼─────────────────────────────┤
   │/etc/systemd/system │ Local configuration         │
   ├────────────────────┼─────────────────────────────┤
   │/run/systemd/system │ Runtime units               │
   ├────────────────────┼─────────────────────────────┤
   │/lib/systemd/system │ Units of installed packages │
   └────────────────────┴─────────────────────────────┘