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:
.service
files describe how you can start/stop a service (amongst other things).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
- This unit file must then be placed in the
/etc/systemd/system
directory# cp fancy-pants.service /etc/systemd/system
- 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
- 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: