Moving to systemd

Having recently set up a self-hosted Ghost server on DigitalOcean using their "one-click application" images, I wanted to upgrade the system since the base image used Ubuntu 14.04. The latest version of Ubuntu at the moment (16.04.1) utilizes systemd. Because of this, the Ghost service won't restart properly since it was configured using the prior Upstart init system in Ubuntu 14.04 and there won't be a corresponding systemd configuration in the new 16.04 system.

This post describes the process of adding a service configuration to utilize the new systemd init system. The example is a self-hosted Ghost blog application but this would apply to any service.

Who is this relevant For?

If you upgraded your Linux system to a systemd based system from a prior System V or Upstart based init system.

For example, after an upgrade, your service might encounter the following error

Failed to start ghost.service: Unit ghost.service failed to load: No such file or directory.  

Here are the steps to achieve the above with detailed explanations of steps and configuration items. Once complete, see the reference section for further reading. Enjoy!

Create systemd service configuration file for ghost

Create the new systemd service configuration file at this location

Description=ghost                         # free-form string describing the unit                      # start after these services are started

Type=simple                               # type of service--see reference section  
WorkingDirectory=/var/www/ghost           # working dir for executed processes  
ExecStart=/usr/bin/npm start --production # executed when this service is started  
ExecStop=/usr/bin/npm stop --production   # executed when this service is stopped  
Restart=always                            # restart when the service process exits, is killed, or a timeout is reached?  
SyslogIdentifier=Ghost                    # prefix for log lines sent to the logging system

[Install]                # service(s) dependent on this service  
Enable new service

Run systemctl enable ghost to enable the service. It should now restart upon reboot of the system.

Check service status

To check the status of your service use the systemctl command--systemctl status ghost