Systemd: Difference between revisions
mNo edit summary |
timers |
||
| Line 1: | Line 1: | ||
== Runlevels vs Targets == | ==Runlevels vs Targets== | ||
Unlike the old init system with different runlevels, Systemd defines sets of services to run inside targets. | Unlike the old init system with different runlevels, Systemd defines sets of services to run inside targets. | ||
| Line 13: | Line 13: | ||
== Journal == | ==Journal== | ||
Systemd keeps tracks of service logs. | Systemd keeps tracks of service logs. | ||
=== Clearing Journal === | ===Clearing Journal=== | ||
Clear all the entries by running: | Clear all the entries by running: | ||
| Line 30: | Line 30: | ||
}} | }} | ||
=== Limiting Log Sizes === | ===Limiting Log Sizes=== | ||
Edit {{code|/etc/systemd/journald.conf}} and define a value for {{code|SystemMaxUse}}. Eg. {{code|1=SystemMaxUse=100M}}. | Edit {{code|/etc/systemd/journald.conf}} and define a value for {{code|SystemMaxUse}}. Eg. {{code|1=SystemMaxUse=100M}}. | ||
== Creating a Service == | |||
== Tasks == | |||
=== Creating a Timer === | |||
You can use systemd to create a timer which can periodically run something, similar to Cronjobs. | |||
A systemd timer can trigger a service. To make use of a systemd timer, you will need to create the service definition (that tells systemd how to run what you want to run) and then the timer definition itself (which tells Systemd when to run it, and how often). | |||
For our service component, we would want a one-shot service that just executes a script: | |||
{{Highlight | |||
| code = [Unit] | |||
Description=Logs system statistics to the systemd journal | |||
Wants=gather-metrics.timer | |||
[Service] | |||
Type=oneshot | |||
ExecStart=/bin/gather-script.sh | |||
[Install] | |||
WantedBy=multi-user.target | |||
| lang = text | |||
}} | |||
For our timer component, we want to run this every 10 minutes which we can define with: | |||
{{Highlight | |||
| code = [Unit] | |||
Description=Gathers metrics from this server | |||
Requires=gather-metrics.service | |||
[Timer] | |||
Unit=gather-metrics.service | |||
OnCalendar=*-*-* *:*:00 | |||
[Install] | |||
WantedBy=timers.target | |||
| lang = text | |||
}} | |||
The <code>OnCalendar</code> specification takes the form of "<code>DOW YYYY-MM-DD HH:MM:SS</code>". An asterisk is a wildcard and matches on any value. The timer will trigger on the next date that matches this format. | |||
Other useful specifications are: | |||
{| class="wikitable" | |||
!OnCalendar | |||
!The job will run... | |||
|- | |||
|<code>*-*-* *:*:00</code> | |||
|Every minute | |||
|- | |||
|<code>*-*-* *:0/10:00</code> | |||
|Every 10 minutes | |||
|- | |||
|<code>*-*-* 00:15:30</code> | |||
|Every day at 12:15:30 AM | |||
|- | |||
|<code>Weekly</code>, or <code>Mon *-*-* 00:00:00</code> | |||
|Every Monday at 00:00:00 | |||
|- | |||
|<code>Mon *-05~03</code> | |||
|On the next Monday which is 3 days from the end of May | |||
|- | |||
|<code>Mon..Fri *-08~04</code> | |||
|On the next weekday which is 4 days from the end of August | |||
|} | |||
===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 {{code|/etc/systemd/system}} containing something like: | 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 {{code|/etc/systemd/system}} containing something like: | ||
| Line 57: | Line 119: | ||
# systemctl enable mag-read | # systemctl enable mag-read | ||
}} | }} | ||
===SystemV-like Init Scripts=== | |||
== 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. | 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. | ||
| Line 87: | Line 145: | ||
A few things of interest. | A few things of interest. | ||
*The {{code|ConditionFileIsExecutable}} will only run the script if it's executable. It basically does a {{code|test -x}} on the file before this continues. | |||
*{{code|RemainAfterExit}} will cause systemd to show the service as active even after the script has finished execution. | |||
In {{code|/usr/local/sbin/custom-startup-initd}}: | In {{code|/usr/local/sbin/custom-startup-initd}}: | ||
| Line 124: | Line 182: | ||
}} | }} | ||
{{Navbox Linux}}[[Category:Linux]] | {{Navbox Linux}} | ||
[[Category:Linux]] | |||
[[Category:LinuxUtilities]] | [[Category:LinuxUtilities]] | ||
Revision as of 23:14, 19 November 2020
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.
Tasks
Creating a Timer
You can use systemd to create a timer which can periodically run something, similar to Cronjobs.
A systemd timer can trigger a service. To make use of a systemd timer, you will need to create the service definition (that tells systemd how to run what you want to run) and then the timer definition itself (which tells Systemd when to run it, and how often).
For our service component, we would want a one-shot service that just executes a script:
[Unit]
Description=Logs system statistics to the systemd journal
Wants=gather-metrics.timer
[Service]
Type=oneshot
ExecStart=/bin/gather-script.sh
[Install]
WantedBy=multi-user.target
For our timer component, we want to run this every 10 minutes which we can define with:
[Unit]
Description=Gathers metrics from this server
Requires=gather-metrics.service
[Timer]
Unit=gather-metrics.service
OnCalendar=*-*-* *:*:00
[Install]
WantedBy=timers.target
The OnCalendar specification takes the form of "DOW YYYY-MM-DD HH:MM:SS". An asterisk is a wildcard and matches on any value. The timer will trigger on the next date that matches this format.
Other useful specifications are:
| OnCalendar | The job will run... |
|---|---|
*-*-* *:*:00
|
Every minute |
*-*-* *:0/10:00
|
Every 10 minutes |
*-*-* 00:15:30
|
Every day at 12:15:30 AM |
Weekly, or Mon *-*-* 00:00:00
|
Every Monday at 00:00:00 |
Mon *-05~03
|
On the next Monday which is 3 days from the end of May |
Mon..Fri *-08~04
|
On the next weekday which is 4 days from the end of August |
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