Systemd: Difference between revisions

From Bitpost wiki
(Created page with "=== Timers === Use these in place of cron. Each one typically does one task. systemd timer services ---------------------- https://www.certdepot.net/rhel7-use-s...")
 
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
Systemd has done serious damage to networkmanager dns cron ntp...  We have to adapt to it, here we go.
=== DNS ===
''UPDATE'': it is working now in Ubuntu 20.04 with my dnsmasq DHCP serving up my 192.168.22.1 nameserver, YAY.
Turn it back on:
sudo su -
rm -f /etc/resolv.conf
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
systemctl unmask systemd-resolved
systemctl enable systemd-resolved
dpkg-reconfigure resolvconf
service network-manager restart
=== Log limit ===
Prevent systemd from casually eating your entire drive with logs by clipping it, here:
$ sudo emacs /etc/systemd/journald.conf
[Journal]
# MBM do not let systemd logs fill drive                                                                                                                                                       
SystemMaxUse=200M
Then reload config:
sudo systemctl daemon-reload
=== NTP ===
DO NOT INSTALL ntp daemon any more, instead we now have systemd-timesyncd.  That relies on systemd-networkd.  Here's what I did on gold (which needed a specific ntp server)...
sudo apt remove ntp
emacs /etc/systemd/timesyncd.conf # if you need to hit a non-standard ntp server
systemctl status systemd-networkd systemd-timedated systemd-timesyncd
sudo timedatectl set-ntp on
sudo systemctl start systemd-networkd systemd-timedated systemd-timesyncd
=== Timers ===
=== Timers ===


Use these in place of cron.  Each one typically does one task.
Use these in place of cron.  Each one typically does one task.


    systemd timer services
[https://www.certdepot.net/rhel7-use-systemd-timers/ systemd timer services]
    ----------------------
    https://www.certdepot.net/rhel7-use-systemd-timers/
create a script to do the work:
 
echo "/usr/sbin/logrotate /etc/logrotate.conf" >/usr/local/sbin/logrotate.sh
    create a script to do the work:
      echo "/usr/sbin/logrotate /etc/logrotate.conf" >/usr/local/sbin/logrotate.sh
 
    create a service file:
      nano /usr/lib/systemd/system/logrotate.service
        [Unit]
        Description=Rotate logs


        [Service]
create a service file:
        Type=simple
nano /usr/lib/systemd/system/logrotate.service
        ExecStart=/usr/local/sbin/logrotate.sh
[Unit]
        User=root
Description=Rotate logs
[Service]
Type=simple
ExecStart=/usr/local/sbin/logrotate.sh
User=root
[Install]
WantedBy=multi-user.target


        [Install]
create a timer file:
        WantedBy=multi-user.target
nano /usr/lib/systemd/system/logrotate.timer
[Unit]
Description=Rotate logs as needed every night at 2am
[Timer]
OnCalendar=*-*-* 02:00:00
Unit=logrotate.service
[Install]
WantedBy=multi-user.target
activate on boot:
# NOTE you must enable the service (even though not run directly), plus the timer
# then start the timer
systemctl enable logrotate     
systemctl enable logrotate.timer
systemctl start logrotate.timer
utils:
systemctl is-enabled ####.timer
systemctl is-active ####.timer  # to see if timer is active and enabled
systemctl start ####      # to run service immediately
systemctl status ####    # nice status output
systemctl daemon-reload  # to restart services after config changes
systemctl list-timers [####*]  # to list timers that start with #####


    create a timer file:
=== Creating a custom managed service ===
      nano /usr/lib/systemd/system/logrotate.timer
        [Unit]
        Description=Rotate logs as needed every night at 2am


        [Timer]
Let's get an official systemd service going!  This example is for rtorrent on bandit.
        OnCalendar=*-*-* 02:00:00
        Unit=logrotate.service


        [Install]
<pre>
        WantedBy=multi-user.target
emacs /etc/systemd/system/rtorrent.service
------------


    activate on boot:
[Unit]
      # NOTE you must enable the service (even though not run directly), plus the timer
Description=rTorrent
      # then start the timer
After=network.target
      systemctl enable logrotate     
[Service]
      systemctl enable logrotate.timer
User=m
      systemctl start logrotate.timer
Type=forking
KillMode=none
ExecStart=/usr/bin/screen -d -m -fa -S rtorrent /usr/bin/rtorrent
ExecStop=/usr/bin/killall -w -s 2 /usr/bin/rtorrent
WorkingDirectory=/home/m/download/torrents/rtorrent
[Install]
WantedBy=default.target


    utils:
--------------
      systemctl is-enabled ####.timer
systemctl enable rtorrent.service
      systemctl is-active ####.timer  # to see if timer is active and enabled
systemctl start rtorrent
      systemctl start ####      # to run service immediately
</pre>
      systemctl status ####    # nice status output
      systemctl daemon-reload  # to restart services after config changes
      systemctl list-timers [####*]  # to list timers that start with #####

Latest revision as of 20:00, 7 November 2022

Systemd has done serious damage to networkmanager dns cron ntp... We have to adapt to it, here we go.

DNS

UPDATE: it is working now in Ubuntu 20.04 with my dnsmasq DHCP serving up my 192.168.22.1 nameserver, YAY.

Turn it back on:

sudo su -
rm -f /etc/resolv.conf
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
systemctl unmask systemd-resolved
systemctl enable systemd-resolved
dpkg-reconfigure resolvconf
service network-manager restart

Log limit

Prevent systemd from casually eating your entire drive with logs by clipping it, here:

$ sudo emacs /etc/systemd/journald.conf 
[Journal]
# MBM do not let systemd logs fill drive                                                                                                                                                         
SystemMaxUse=200M

Then reload config:

sudo systemctl daemon-reload

NTP

DO NOT INSTALL ntp daemon any more, instead we now have systemd-timesyncd. That relies on systemd-networkd. Here's what I did on gold (which needed a specific ntp server)...

sudo apt remove ntp
emacs /etc/systemd/timesyncd.conf # if you need to hit a non-standard ntp server
systemctl status systemd-networkd systemd-timedated systemd-timesyncd
sudo timedatectl set-ntp on
sudo systemctl start systemd-networkd systemd-timedated systemd-timesyncd


Timers

Use these in place of cron. Each one typically does one task.

systemd timer services

create a script to do the work:

echo "/usr/sbin/logrotate /etc/logrotate.conf" >/usr/local/sbin/logrotate.sh

create a service file:

nano /usr/lib/systemd/system/logrotate.service
[Unit]
Description=Rotate logs

[Service]
Type=simple
ExecStart=/usr/local/sbin/logrotate.sh
User=root

[Install]
WantedBy=multi-user.target

create a timer file:

nano /usr/lib/systemd/system/logrotate.timer
[Unit]
Description=Rotate logs as needed every night at 2am

[Timer]
OnCalendar=*-*-* 02:00:00
Unit=logrotate.service

[Install]
WantedBy=multi-user.target

activate on boot:

# NOTE you must enable the service (even though not run directly), plus the timer
# then start the timer
systemctl enable logrotate       
systemctl enable logrotate.timer
systemctl start logrotate.timer

utils:

systemctl is-enabled ####.timer
systemctl is-active ####.timer   # to see if timer is active and enabled
systemctl start ####      # to run service immediately
systemctl status ####     # nice status output
systemctl daemon-reload   # to restart services after config changes
systemctl list-timers [####*]  # to list timers that start with #####

Creating a custom managed service

Let's get an official systemd service going! This example is for rtorrent on bandit.

emacs /etc/systemd/system/rtorrent.service
------------

[Unit]
Description=rTorrent
After=network.target
[Service]
User=m
Type=forking
KillMode=none
ExecStart=/usr/bin/screen -d -m -fa -S rtorrent /usr/bin/rtorrent
ExecStop=/usr/bin/killall -w -s 2 /usr/bin/rtorrent
WorkingDirectory=/home/m/download/torrents/rtorrent
[Install]
WantedBy=default.target

--------------
systemctl enable rtorrent.service
systemctl start rtorrent