systemd🔗
Although some folks hate systemd :). I am more keen on solving my problem rather than getting into that debate. I still use init on my freeBSD instances.
Since I occasionally encounter system running systemd, I think there is no hard in getting familiar with it.
Basic Commands🔗
# start a service
sudo systemctl start servicename
# stop a service
sudo systemctl stop servicename
# restart a service
sudo systemctl restart servicename
# reload a service
sudo systemctl reload servicename
# enable a service
sudo systemctl enable servicename
# disable a service
sudo systemctl disable servicename
# check service status
sudo systemctl status servicename
Sytem Management🔗
Creating a Custom Service🔗
Create /etc/systemd/system/myservice.service
[Unit]
Description=My Custom Service
After=network.target
[Service]
ExecStart=/usr/bin/myscript.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
# reload daemon
sudo systemctl daemon-reload
# enable the service
sudo systemctl start myservice
sudo systemctl enable myservice
Timers (Cron Alternatives)🔗
Create a timer file : /etc/systemd/system/mytimer.timer
[Unit]
Description=Run My Script Daily
[Timer]
OnCalendar=daily
Persistent=true
[Install]
WantedBy=timers.target
Create a Service File : /etc/systemd/system/mytimer.service
Debugging🔗
# view logs
journalctl -u servicename
# follow logs
journalctl -u servicename -f
# filter by time
journalctl --since "2023-10-01" --until "2023-10-02"
Best Practies🔗
-
Limit Service Permissions: Use
User
andGroup
directives in service files
-
Sandbox Services: Use
ProtectSystem
andProtectHome
- Disable Unused services:
sudo systemctl disable servicename
-
Automate Service Restarts: Use
Restart=always
in service files
- Identify rogue services during boottime:
systemd-analyze blame
Example Service to Send Pushover Notification on Server Reboot🔗
Edit : /etc/systemd/system/multi-user.target.wants/reboot_ntf.service
[Unit]
Description=ntf reboot script
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/bin/bash -c 'sleep 2 && /usr/local/bin/ntf send -t REBOOT minetest.in just rebooted! > /var/log/reboot_ntf.log 2>&1'
Type=oneshot
RemainAfterExit=true
Environment=HOME=/root/
[Install]
WantedBy=multi-user.target