Skip to content

unix_systemd

Andrea Paolini edited this page Jan 2, 2021 · 1 revision

Systemd cheatsheet

For an introduction see http://www.enricozini.org/blog/2017/debian/systemd-01-intro/

systemctl

"service"

systemctl stop myservice
systemctl start myservice
systemctl restart myservice
systemctl reload myservice
systemctl status myservice

"chkconfig"

systemctl enable myservice
systemctl disable myservice
systemctl is-enabled myservice

"chkconfig --list"

systemctl list-unit-files --type=service

or simply:

systemctl

Reload systemd

Reload systemd, scanning for new or changed units:

systemctl daemon-reload

Restart unit on failure

[Service]
Restart=on-failure
RestartSec=30

cfr systemctl edit ...

Specifiers

AKA builtin "variables" for unit files(i.e. hostname): see systemd.unit(5)

journalctl

# Logs from last boot
journalctl -b

# Follow logs
journalctl -f

# From kernel
journalctl -k

# From a specific binary
journalctl /usr/bin/pippo

# In reverse order (newest first)
journalctl -r

systemd units in Docker

This requires a fairly recent version of CentOS7 or CentOS8

  • If using SElinux enable setsebool -P container_manage_cgroup 1 in the host
  • In the Dockerfile:
    • Use the latest centos:7 or centos:8 base image
    • Set the environment variable container=docker
    • Set STOPSIGNAL to SIGRTMIN+3 to allow for a clean stop
    • use /sbin/init as a CMD

Example:

FROM centos:7
ENV container docker
STOPSIGNAL SIGRTMIN+3
RUN yum -y install httpd
systemctl enable httpd
EXPOSE 80
CMD [ "/sbin/init" ]

Clone this wiki locally