atom tldr

default services are in /lib/systemd/… but you can override in /etc/systemd/…

Why this and not pm2? cuz it’s same same but available on linux in general.

vim /etc/systemd/system/name.service
[Unit]
Description=ROT13 demo service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=centos
ExecStart=/usr/bin/env php /path/to/server.php
 
[Install]
WantedBy=multi-user.target

Anatomy of systemd

[Section]
Directive=value
Directive2= // setting it to nothing is unsetting it, useful for overrides

[Unit] section directives (First one)

  • Description= description returned by many systemd tools
  • Documentation= path for docuemntation, useful for man command
  • Requires= lists units on which this unit depend. they must all be active first, and they start in parallel with this unit
  • BindsTo= same as requires, but if the required units die, this unit dies too
  • Before&After= variations on Requires
  • Assert= check arbitrary stuff e.g env variables to start

[Service] section directive (Middle)

Provides service specific configurations

  • Type= (simple, forking, oneshot, dbus, notify, idle) Service Types
  • ExecStart= path and arguments for service
  • ExecStartPre, ExecStartPost= Commands to be run before or after the service
  • ExecReload, ExecStop, = the reload/stop command overrides
  • Restart= When to restart it (“always”, “on-success”, “on-failure”, “on-abnormal”, “on-abort”, or “on-watchdog”eq.se
  • WorkingDirectory= self explanatory

[Install] section directives (Last one)

Similar to Unit in that it has WantedBy, RequiredBy. But in install, they are related to weather or not enable/disable command will work or not, instead of weather the service runs or not

See Also: