Skip to content

Instantly share code, notes, and snippets.

@neenaoffline
Last active May 25, 2016 11:48
Show Gist options
  • Save neenaoffline/717fcd6bfe587139908d7712881c06f0 to your computer and use it in GitHub Desktop.
Save neenaoffline/717fcd6bfe587139908d7712881c06f0 to your computer and use it in GitHub Desktop.
Creating Systemd services

Creating a Systemd service

Systemd unit files are resources that Systemd recognizes and operates on. There are several types of unit files, two of which are immediately relevant to us:

  1. .service files describe how you can start/stop a service (amongst other things)
  2. .target files function similar to how run levels do. They act as synchronization points when booting up or changing states

Below is a simple fancy-pants.service file that will start the fancy-pants app.

[Unit]
Description=Fancy pants service

[Service]
Type=simple
PIDFile=/var/run/fancy-pants.pid
ExecStart=/opt/fancy-pants/bin/app --now
ExecStop=/usr/bin/kill -15 $(cat /var/run/fancy-pants.pid)
Environment=PORT=8080
Restart=always

[Install]
WantedBy=default.target

To set this up with systemd, proceed with the following steps

  1. This unit file must then be placed in the /etc/systemd/system directory
    # cp fancy-pants.service /etc/systemd/system
    
  2. The unit file must be enabled. Doing so copies the .service file into the .target directories that either wants or requires it. These are specified in the [Install] section of the unit file. A .target is approximately what used to be run levels. The default.target usually links to the multi-user.target file. To enable the service one must run...
    # systemctl enable foo
    
  3. Now, the service will be started when the default.target is initialized, or, it can be manually initialized by running...
    # systemctl start foo
    

Some documentation to better understand all of this:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment