Skip to main content
Notes by Peter Galonza(Пётр Галонза)
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

SystemD

Limits

/etc/systemd/system.conf, /etc/systemd/user.conf, /etc/systemd/<systemd_unit>/override.conf

DefaultLimitNOFILE=

/lib/systemd/system/, /etc/systemd/*, /usr/lib/systemd/system/

LimitNOFILE=

override.conf

mkdir /etc/systemd/system/service_name.service.d/
[Service]
LimitNOFILE=100000

Show limits

import platform

if 'linux' in platform.system().lower():
    import resource  # Linux only

    limit_nofile = resource.getrlimit(resource.RLIMIT_NOFILE)
    limit_nproc = resource.getrlimit(resource.RLIMIT_NPROC)

    print ('Max number of opened files allowed:', limit_nofile)
    print ('Max number of processes allowed', limit_nproc)

Show problems

systemctl --failed

Get pid

systemctl status systemd-modules-load

Nspawn

Create container system files

mount rootfs.img /var/lib/machines/<container name>

Start container

systemctl start systemd-nspawn@<container name>

Connect to container

systemd-run -t -M <container name> /bin/bash

Show status

machinectl status <container name>

Start on boot

machinectl enable <container name>

Set quotas

systemctl set-property systemd-nspawn@<container name> CPUQuota=200%
systemctl set-property systemd-nspawn@<container name> MemoryMax=2G

Units

Unit security analyze

systemd-analyze security <unit name>

OOMKiller

OOMScoreAdjust=1000
ExecStartPost=/bin/bash -c "echo <memory>G > /sys/fs/cgroup/memory/system.slice/php-fpm.service/memory.memsw.limit_in_bytes"
ExecStartPost=/bin/bash -c "echo 0 > /sys/fs/cgroup/memory/system.slice/php-fpm.service/memory.swappiness"
MemoryLimit=<memory>G

Create unit with wrapper /etc/systemd/system/<service name>.service

[Unit]
Description=<description>
After=syslog.target network.target
[Service]
SuccessExitStatus=143
User=<username>
Group=<usergroup>

Type=simple

ExecStart=</path to wrapper>
ExecStop=/bin/kill -15 $MAINPID

[Install]
WantedBy=multi-user.target
#!/bin/bash

JAVA_HOME=<java path>
WORKDIR=<service work dir>
JAVA_OPTIONS="<java options>"
APP_OPTIONS="<application options>"

cd $WORKDIR
eval exec "${JAVA_HOME}/bin/java" $JAVA_OPTIONS -jar <jar file>.jar $APP_OPTIONS

Docker

[Unit]
Description=<description>
After=docker.service
Requires=docker.service

[Service]
TimeoutStartSec=0
Restart=always
ExecStartPre=-/usr/bin/docker exec %n stop
ExecStartPre=-/usr/bin/docker rm %n
ExecStartPre=/usr/bin/docker pull <docker image>
ExecStart=/usr/bin/docker run --rm --name %n \
    <docker image>

[Install]
WantedBy=default.target

Journalctl

Unit logs in real time

journalctl -fu <unit_name>

Show problems by pid

journalctl _PID=

Show without less

journalctl --no-pager

Systemd-analyze

Show load time

systemd-analyze

Show load time in detail

systemd-analyze blame

Write load information in svf file

systemd-analyze plot > graph.svf

Systemd-tmpfiles

Configuration directories

/usr/lib/tmpfiles.d/.conf
/run/tmpfiles.d/.conf
/etc/tmpfiles.d/*.conf

SYSTEMCTL

Systemd reload

systemctl daemon-reload

Restart network manager

systemctl restart NetworkManager

Masked/unmasked, completely disabled service

sudo systemctl mask service_name
sudo systemctl unmask service_name

Login manager

Show sessions

loginctl list-sessions

Kill session

loginctl kill-session <session id>

Kill user

loginctl terminate-user <username>