How to setup Systemd Services

Systemctl commands

Starting and Stopping Services

$ systemctl start application.service <or> systemctl start application # start up the application service
$ systemctl stop application.service # stop currently running service
$ systemctl daemon-reload # reload `systemd` process

Restarting and Reloading

$ systemctl restart application.service # restart running service
$ systemctl reload application.service # if app can reload its config files(without restarting), reload
$ systemctl reload-or-restart application.service # if you're unsure whether app has ability to reload its configuration, reload in-place or restart if new config is picked up

Enabling and Disabling

$ systemctl enable application.service # start service automatically at boot
$ systemctl disable application.service # disable service from starting automatically

Enable will create symbolic link in /lib/systemd/system or /etc/systemd/system into location where systemd looks for autostart files(usually at /etc/systemd/system/some_target.target.wants)

Disable will remove symbolic links.
P.S. Only enabling the service will not start the service at boot. Has to start it as well.

Checking Status of services

$ systemctl status application.service 
$ systemctl is-active application.service # checking if service is active
$ systemctl is-enabled application.service # checking if service is enabled
$ systemctl is-failed application.service # checking if service is failed

System state overview

$ systemctl list-units (or) systemctl # list of all units that systemd has active on system
$ systemctl list-unit-files # list every available unit files

Displaying Unit file

$ systemctl cat sshd.service

Displaying dependencies

$ systemctl list-dependencies sshd.service 

Masking and Unmasking

$ systemctl mask nginx.service # makes the service completely unstartable
$ systemctl unmask nginx.service 

Systemctl Process commands

$ systemctl get-default # returns default target that systemd uses when booting the system
$ systemctl set-default graphical.target # sets target to boot into by default
$ systemctl list-unit-files --type=target # list available targets

Looking at logs from systemctl

$ journalctl
  • -u, --unit="Service-name" # Returns logs for service-name

Systemd Caveats

  • Files in /etc/systemd/system take precedence over /lib/systemd/system