Difference between revisions of "Linux: SystemD"

From RHS Wiki
Jump to navigation Jump to search
Line 1: Line 1:
# List all running services
+
== List all running services ==
 
  sytemctl
 
  sytemctl
# Start/stop or enable/disable services
+
== Start/stop or enable/disable services ==
 
  systemctl start foo.service
 
  systemctl start foo.service
 
  systemctl stop foo.service
 
  systemctl stop foo.service
Line 8: Line 8:
 
  systemctl enable foo.service
 
  systemctl enable foo.service
 
  systemctl disable foo.service
 
  systemctl disable foo.service
# Check if service is enabled
+
== Check if service is enabled ==
 
  systemctl is-enabled foo.service; echo $?
 
  systemctl is-enabled foo.service; echo $?
  
Line 44: Line 44:
 
WantedBy=multi-user.target
 
WantedBy=multi-user.target
 
</nowiki>
 
</nowiki>
 +
 +
 +
== Automatic restart ==
 +
Add the following lines to your service file at Service block
 +
Restart=always
 +
RestartSec=3
 +
 +
watch "ps -ef|grep service"

Revision as of 15:50, 9 October 2018

List all running services

sytemctl

Start/stop or enable/disable services

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

systemctl is-enabled foo.service; echo $?

Source: Useful SystemD Commands

Log to syslog

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

[Unit]
Description=Tenkaichi Vegeta-server (Tentacle)
After=network.target

[Service]
User=tenkaichi
Group=tenkaichi
EnvironmentFile=/tenkaichi/vegeta-server/cfg/environment.cfg
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=Vegeta-Tentacle
Restart=always

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

Add the following lines to your service file at Service block

Restart=always
RestartSec=3

watch "ps -ef|grep service"