Linux: SystemD

From RHS Wiki
Revision as of 11:01, 29 March 2019 by Rafahsolis (talk | contribs) (→‎/etc/systemd/system/example.service)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

List all running services[edit]

sytemctl
watch "ps -ef|grep service"

Start/stop or enable/disable services[edit]

systemctl start foo.service
systemctl stop foo.service
systemctl restart foo.service
systemctl status foo.service
systemctl enable foo.service
systemctl disable foo.service

Check if service is enabled[edit]

systemctl is-enabled foo.service; echo $?

Source: Useful SystemD Commands

Log to syslog[edit]

Use the following properties in your systemd service unit file:

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=<your program identifier> # without any quote

Then, assuming your distribution is using rsyslog to manage syslogs, create a file in /etc/rsyslog.d/<new_file>.conf with the following content:

if $programname == '<your program identifier>' then /path/to/log/file.log
if $programname == '<your program identifier>' then ~

/etc/systemd/system/example.service[edit]

[Unit]
Description=Tenkaichi Vegeta-server (Tentacle)
After=network.target
 
[Service]
User=tenkaichi
Group=tenkaichi
WorkingDirectory=/home/tenkaichi/vegetas-server
EnvironmentFile=/tenkaichi/vegeta-server/cfg/environment.cfg
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=Vegeta-Tentacle
Restart=always
RestartSec=3

ExecStart=/usr/local/bin/tentacle_server -o -a ${SERVER_IP} -p ${PORT} -s ${INPUT_DIR} -v -m ${MAX_SIZE} -e ${SSL_CERT} -k ${SSL_CERTKEY}

[Install]
WantedBy=multi-user.target
 

Automatic restart[edit]

Add the following lines to your service file at Service block

[Service]
...
Restart=always
RestartSec=3
...

Wait Online[edit]

[Unit]
  Wants=network-online.target
  After=network.target network-online.target

With NetworkManager[edit]

systemctl enable NetworkManager-wait-online

ExecStart examples[edit]

Gunicorn[edit]
ExecStart=/virtualenv/path/bin/gunicorn -w3 --timeout 300 --access-logfile /var/log/gunicorn/app-access.log --error-logfile /var/log/gunicorn/app-error.log --bind unix:/sockets/app-socket.sock App.wsgi

Flask (superset)[edit]

ExecStart=/home/superset/.virtualenvs/superset/bin/gunicorn -w 3 -k gevent --timeout 120 -b unix:/home/superset/.superset/superset.sock --limit-request-line 0 --limit-request-field_size 0 --statsd-host localhost:8125 superset:app