Systemd: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 7: | Line 7: | ||
The service command invokes a script in /etc/init.d. | The service command invokes a script in /etc/init.d. | ||
=Service commands= | |||
;service --status-all | ;service --status-all | ||
:Run all scripts with the 'status' option. Tells you which services are up. | :Run all scripts with the 'status' option. Tells you which services are up. | ||
;service <service> | |||
:Show all options for service, usually 'start|stop|restart|reload|status' | |||
;service <service> status | |||
:Show detailed status for <service> | |||
;systemctl enable <service> | |||
:Make the service start at boot-time | |||
;systemctl list-units | ;systemctl list-units | ||
:Show all known units. Alternative for <code>service --status-all</code> that does not work on modern RedHat versions. | :Show all known units. Alternative for <code>service --status-all</code> that does not work on modern RedHat versions. | ||
;systemctl list-timers | ;systemctl list-timers | ||
:Show all active timers | :Show all active timers | ||
| Line 51: | Line 56: | ||
;systemctl daemon-reload | ;systemctl daemon-reload | ||
:After changing a configuration file reload it | :After changing a configuration file reload it | ||
;systemctl mask UNIT> | ;systemctl mask <UNIT> | ||
:Link the timer to /dev/null effectively disabling it | :Link the timer to /dev/null effectively disabling it | ||
;systemd-analyze [blame|critical-chain] | ;systemd-analyze [blame|critical-chain] | ||
:Utility that shows what is holding up the boot process | :Utility that shows what is holding up the boot process | ||
==Create a service== | |||
/etc/systemd/system/daemontest.service | |||
<syntaxhighlight lang=text> | |||
[Unit] | |||
Description=Test daemon | |||
After=mnt-www.mount # Start after /mnt/www is mounted | |||
[Service] | |||
Type=simple | |||
User=<username> # User dat will run the program | |||
ExecStart=<path to>/daemontest.py | |||
Restart=on-failure | |||
[Install] | |||
WantedBy=default.target | |||
</syntaxhighlight> | |||
<syntaxhighlight lang=bash> | |||
systemctl daemon-reload | |||
systemctl enable daemontest.service | |||
</syntaxhighlight> | |||
<syntaxhighlight lang=python> | |||
#!/usr/bin/env python3 | |||
import daemon | |||
from time import sleep | |||
from datetime import datetime | |||
outfile = '/tmp/testdaemon_{}.out' | |||
def main(): | |||
count = 5 | |||
while count: | |||
with open(outfile.format(count),'w') as fh: | |||
count -= 1 | |||
count2 = 3 | |||
while count2: | |||
count2 -= 1 | |||
fh.write(f"{datetime.now()} Dit is een test\n") | |||
sleep(10) | |||
with daemon.DaemonContext(): | |||
main() | |||
</syntaxhighlight> | |||
=System messages= | |||
The command journalctl shows system services messages | |||
;journalctl | |||
:Show all from old to new | |||
;journalctl -u <servicename> | |||
:Show all for <servicename> | |||
;journalctl -S <nowiki><YYYY-mm-dd HH:MM:SS> -U <YYYY-mm-dd HH:MM:SS></nowiki> | |||
;journalctl -S yesterday | |||
:Show messages since -S-date/time until -U-date/time | |||
Latest revision as of 18:20, 25 September 2024
Replaces initd. It should be more efficient. Honestly I don't see the benefits.
It also provides timed execution like cron does equally well.
The service command invokes a script in /etc/init.d.
Service commands
- service --status-all
- Run all scripts with the 'status' option. Tells you which services are up.
- service <service>
- Show all options for service, usually 'start|stop|restart|reload|status'
- service <service> status
- Show detailed status for <service>
- systemctl enable <service>
- Make the service start at boot-time
- systemctl list-units
- Show all known units. Alternative for
service --status-allthat does not work on modern RedHat versions. - systemctl list-timers
- Show all active timers
E.g for certbot (letsencrypt)
$ systemctl list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
do 2018-11-29 00:00:00 CET 2h 5min left wo 2018-11-28 12:00:01 CET 9h ago certbot.timer certbot.service
2 timers listed.
Pass --all to see loaded but inactive timers, too.
$ cat /lib/systemd/system/certbot.timer
[Unit]
Description=Run certbot twice daily
[Timer]
OnCalendar=*-*-* 00,12:00:00
Persistent=true
[Install]
WantedBy=timers.target
$ cat /lib/systemd/system/certbot.service
[Unit]
Description=Certbot
Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
Documentation=https://letsencrypt.readthedocs.io/en/latest/
[Service]
Type=oneshot
ExecStartPre=/usr/bin/perl -e 'sleep int(rand(3600))'
ExecStart=/usr/bin/certbot -q renew
PrivateTmp=true
- systemctl daemon-reload
- After changing a configuration file reload it
- systemctl mask <UNIT>
- Link the timer to /dev/null effectively disabling it
- systemd-analyze [blame|critical-chain]
- Utility that shows what is holding up the boot process
Create a service
/etc/systemd/system/daemontest.service
[Unit]
Description=Test daemon
After=mnt-www.mount # Start after /mnt/www is mounted
[Service]
Type=simple
User=<username> # User dat will run the program
ExecStart=<path to>/daemontest.py
Restart=on-failure
[Install]
WantedBy=default.target
systemctl daemon-reload
systemctl enable daemontest.service
#!/usr/bin/env python3
import daemon
from time import sleep
from datetime import datetime
outfile = '/tmp/testdaemon_{}.out'
def main():
count = 5
while count:
with open(outfile.format(count),'w') as fh:
count -= 1
count2 = 3
while count2:
count2 -= 1
fh.write(f"{datetime.now()} Dit is een test\n")
sleep(10)
with daemon.DaemonContext():
main()
System messages
The command journalctl shows system services messages
- journalctl
- Show all from old to new
- journalctl -u <servicename>
- Show all for <servicename>
- journalctl -S <YYYY-mm-dd HH:MM:SS> -U <YYYY-mm-dd HH:MM:SS>
- journalctl -S yesterday
- Show messages since -S-date/time until -U-date/time