Systemd: Difference between revisions
Moved Fedora 20 notes into runlevels and targets |
|||
| Line 1: | Line 1: | ||
== Runlevels vs Targets == | |||
Unlike the old init system with different runlevels, Systemd defines sets of services to run inside targets. | |||
Instead of using {{code|init 5}}: | |||
{{highlight|lang=terminal|code= | |||
# systemctl isolate graphical.target | |||
}} | |||
or {{code|init 3}}: | |||
{{highlight|lang=terminal|code= | |||
# systemctl isolate multi-user.target | |||
}} | |||
== Journal == | == Journal == | ||
Systemd keeps tracks of service logs. | Systemd keeps tracks of service logs. | ||
Revision as of 20:58, 12 May 2019
Runlevels vs Targets
Unlike the old init system with different runlevels, Systemd defines sets of services to run inside targets.
Instead of using init 5:
# systemctl isolate graphical.target
or init 3:
# systemctl isolate multi-user.target
Journal
Systemd keeps tracks of service logs.
Clearing Journal
Clear all the entries by running:
# journalctl --vacuum-time=1seconds
Or
# journalctl --vacuum-size=1M
Limiting Log Sizes
Edit /etc/systemd/journald.conf and define a value for SystemMaxUse. Eg. SystemMaxUse=100M.
Creating a Service
Suppose you have a program that you want started when the system boots. To have systemd start the program as a service, create a new service file in /etc/systemd/system containing something like:
# cat /etc/systemd/system/mag-read.service
[Unit]
Description=Mag card reader
[Install]
WantedBy=multi-user.target
[Service]
ExecStart=/root/scripts/mag.sh
Restart=always
This service file will execute mag.sh which will start a process that sits and waits for input from a mag-strip card reader and will automatically restart it if it stops.
Once the file has been created, reload systemd and enable the service:
# systemctl daemon-reload # Reloads the service files so systemd is aware of the new one
# systemctl enable mag-read
SystemV-like Init Scripts
If you have custom packages that make use of the old SystemV startup scripts, you can use the following files to make use of such scripts.
In /usr/lib/systemd/system/startup.service
[Unit]
Description=Custom SystemV-like Startup
After=network.target
ConditionFileIsExecutable=/usr/local/sbin/custom-startup-initd
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/custom-startup-initd
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
[Install]
WantedBy=multi-user.target
A few things of interest.
- The
ConditionFileIsExecutablewill only run the script if it's executable. It basically does atest -xon the file before this continues. RemainAfterExitwill cause systemd to show the service as active even after the script has finished execution.
In /usr/local/sbin/custom-startup-initd:
#!/bin/sh
StartupDir="/usr/local/boot/startup"
# Run all scripts starting with 'S'
for i in `ls $StartupDir/S*` ; do
ScriptName=`basename $i`
sh $StartupDir/$ScriptName
done
# Success exit code.
exit 0
Make the directory:
# mkdir -p /usr/local/boot/startup/
Then place your scripts in the startup directory starting with the letter 'S'.
Enable the new service by running:
# systemctl enable startup
# systemctl start startup
Verify this:
# systemctl status startup.service