This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
linux:linux_systemd [2021/11/05 16:03] manu |
linux:linux_systemd [2024/06/26 15:49] (current) manu |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Manage services with systemd ===== | + | ====== Manage services with systemd ====== |
Systemd is now used on most Linux distribution: Redhat, Suse, Ubuntu... | Systemd is now used on most Linux distribution: Redhat, Suse, Ubuntu... | ||
Line 23: | Line 23: | ||
## List all services | ## List all services | ||
<cli prompt='$'> | <cli prompt='$'> | ||
- | $ systemctl list-units --type=service | + | $ systemctl list-units --type=service |
+ | $ systemctl list-units --type mount | ||
</cli> | </cli> | ||
Line 100: | Line 101: | ||
● ├─irqbalance.service | ● ├─irqbalance.service | ||
</cli> | </cli> | ||
+ | |||
==== To enable NetworkManager, use: ==== | ==== To enable NetworkManager, use: ==== | ||
=== First, stop the running service: === | === First, stop the running service: === | ||
- | systemctl is-active network.service && \ | + | <cli prompt='$'> |
- | systemctl stop network.service | + | $ systemctl is-active network.service && systemctl stop network.service |
+ | </cli> | ||
=== Enable the NetworkManager service: === | === Enable the NetworkManager service: === | ||
Line 118: | Line 121: | ||
Check whether a service is already enabled or not: | Check whether a service is already enabled or not: | ||
- | # systemctl is-enabled foo.service; echo $? | + | <cli prompt='$'> |
+ | $ systemctl is-enabled foo.service; echo $? | ||
+ | </cli> | ||
0 indicates that it is enabled. 1 indicates that it is disabled | 0 indicates that it is enabled. 1 indicates that it is disabled | ||
=== Start the NetworkManager service (via alias link): === | === Start the NetworkManager service (via alias link): === | ||
- | systemctl start network.service | + | <cli prompt='$'> |
+ | $ systemctl start network.service | ||
+ | </cli> | ||
==== To disable NetworkManager, use: ==== | ==== To disable NetworkManager, use: ==== | ||
Stop the running service: | Stop the running service: | ||
- | systemctl is-active network.service && \ | + | <cli prompt='$'> |
- | systemctl stop network.service | + | $ systemctl is-active network.service && \ |
+ | systemctl stop network.service | ||
+ | </cli> | ||
Disable the NetworkManager service: | Disable the NetworkManager service: | ||
- | systemctl disable NetworkManager.service | + | <cli prompt='$'> |
+ | $ systemctl disable NetworkManager.service | ||
+ | </cli> | ||
Start the /etc/init.d/network service: | Start the /etc/init.d/network service: | ||
- | systemctl start network.service | + | <cli prompt='$'> |
+ | $ systemctl start network.service | ||
+ | </cli> | ||
all active and failed services: | all active and failed services: | ||
- | systemctl list-units --type service --state running,failed | + | <cli prompt='$'> |
- | | + | $ systemctl list-units --type service --state running,failed |
+ | </cli> | ||
==== To query the currently selected service, use: ==== | ==== To query the currently selected service, use: ==== | ||
- | <code> | + | <cli prompt='#'> |
graphite:~ # systemctl status network.service | graphite:~ # systemctl status network.service | ||
NetworkManager.service - Network Manager | NetworkManager.service - Network Manager | ||
Line 151: | Line 164: | ||
Active: inactive (dead) | Active: inactive (dead) | ||
CGroup: name=systemd:/system/NetworkManager.service | CGroup: name=systemd:/system/NetworkManager.service | ||
- | </code> | + | </cli> |
==== Start / Stop a service ==== | ==== Start / Stop a service ==== | ||
- | | + | |
- | sytemctl stop network.service | + | <cli prompt='#'> |
- | sytemctl start network.service | + | # sytemctl stop network.service |
- | sytemctl status network.service | + | # sytemctl start network.service |
+ | # sytemctl status network.service | ||
+ | </cli> | ||
| | ||
==== Create a new service ==== | ==== Create a new service ==== | ||
Line 220: | Line 235: | ||
It should then start automatically after rebooting the machine. | It should then start automatically after rebooting the machine. | ||
+ | |||
+ | List service dependancy | ||
+ | <cli prompt='#'> | ||
+ | [root@lnx01 system]# systemctl list-dependencies boot.mount | ||
+ | boot.mount | ||
+ | ● ├─-.mount | ||
+ | ● ├─dev-disk-by\x2duuid-3d0d7370\x2d3c77\x2d462a\x2d86a6\x2dd160d54a7fa6.device | ||
+ | ● ├─system.slice | ||
+ | ● └─systemd-fsck@dev-disk-by\x2duuid-3d0d7370\x2d3c77\x2d462a\x2d86a6\x2dd160d54a7fa6.service | ||
+ | </cli> | ||
+ | |||
+ | Add a dependancy to start after network and local filesystem mount | ||
+ | <cli prompt='#'> | ||
+ | [root@lnx01 system]# cat /etc/systemd/system/keycloak.service | ||
+ | [Unit] | ||
+ | Description=keycloak | ||
+ | After=network.target local-fs.target | ||
+ | Requires=local-fs.target | ||
+ | |||
+ | [Service] | ||
+ | #Type=idle | ||
+ | Type=forking | ||
+ | #User=keycloak | ||
+ | #Group=keycloak | ||
+ | ExecStart=/usr/lib/systemd/system/keycloak start | ||
+ | TimeoutStartSec=600 | ||
+ | TimeoutStopSec=600 | ||
+ | |||
+ | [Install] | ||
+ | WantedBy=multi-user.target | ||
+ | </cli> | ||
==== Change default target to boot (runlevel) ==== | ==== Change default target to boot (runlevel) ==== | ||
Line 293: | Line 339: | ||
==== Systemd analyse ==== | ==== Systemd analyse ==== | ||
+ | |||
<cli prompt='#'> | <cli prompt='#'> | ||
# systemd-analyze | # systemd-analyze | ||
Line 322: | Line 369: | ||
└─network.target @31.558s | └─network.target @31.558s | ||
└─network.service @30.819s +738ms | └─network.service @30.819s +738ms | ||
+ | </cli> | ||
+ | |||
+ | |||
+ | |||
+ | Realtime stats for services | ||
+ | <cli prompt='#'> | ||
+ | [root@lnx01 system]# systemd-cgtop | ||
+ | Control Group Tasks %CPU Memory Input/s Output/s | ||
+ | / 918 - 4.8G - - | ||
+ | /system.slice 678 - 3.6G - - | ||
+ | /system.slice/NetworkManager.service 3 - 33.2M - - | ||
+ | /system.slice/abrt-oops.service 1 - 11.0M - - | ||
+ | /system.slice/abrt-xorg.service 1 - 11.1M - - | ||
+ | /system.slice/abrtd.service 3 - 21.6M - - | ||
+ | /system.slice/atd.service 1 - 2.8M - - | ||
+ | ... | ||
+ | </cli> | ||
+ | |||
+ | Journal config file (2 ways to list it) | ||
+ | <cli prompt='#'> | ||
+ | [root@lnx01 system]# cat /etc/systemd/journald.conf | ||
+ | [root@lnx01 system]# systemd-analyze cat-config systemd/journald.conf | ||
+ | [Journal] | ||
+ | Storage=persistent | ||
+ | Compress=yes | ||
+ | #Seal=yes | ||
+ | #SplitMode=uid | ||
+ | #SyncIntervalSec=5m | ||
+ | #RateLimitIntervalSec=30s | ||
+ | #RateLimitBurst=10000 | ||
+ | #SystemMaxUse= | ||
+ | #SystemKeepFree= | ||
+ | #SystemMaxFileSize= | ||
+ | #SystemMaxFiles=100 | ||
+ | #RuntimeMaxUse= | ||
+ | #RuntimeKeepFree= | ||
+ | #RuntimeMaxFileSize= | ||
+ | #RuntimeMaxFiles=100 | ||
+ | #MaxRetentionSec= | ||
+ | MaxFileSec=1month | ||
+ | </cli> | ||
+ | |||
+ | ====== Manage service with System V (5) ====== | ||
+ | |||
+ | Old way to define a service, based on runlevel. Still used by some linux distribution, but most used Systemd. | ||
+ | |||
+ | List all services | ||
+ | <cli prompt='#'> | ||
+ | [root@lnx01 system]# chkconfig --list | ||
+ | |||
+ | Note: This output shows SysV services only and does not include native | ||
+ | systemd services. SysV configuration data might be overridden by native | ||
+ | systemd configuration. | ||
+ | |||
+ | If you want to list systemd services use 'systemctl list-unit-files'. | ||
+ | To see services enabled on particular target use | ||
+ | 'systemctl list-dependencies [target]'. | ||
+ | |||
+ | keycloak-homol 0:off 1:off 2:on 3:on 4:on 5:on 6:off | ||
+ | keycloak-labo 0:off 1:off 2:on 3:on 4:on 5:on 6:off | ||
+ | keycloak-recette 0:off 1:off 2:on 3:on 4:on 5:on 6:off | ||
+ | </cli> | ||
+ | |||
+ | Status of a service | ||
+ | <cli prompt='#'> | ||
+ | # service sshd status | ||
+ | # service --status-all | ||
+ | </cli> | ||
+ | |||
+ | enable or disable | ||
+ | <cli prompt='#'> | ||
+ | # chkconfig httpd off | ||
+ | # chkconfig ntpd on | ||
+ | </cli> | ||
+ | |||
+ | enable for specific level | ||
+ | <cli prompt='#'> | ||
+ | # chkconfig --level 3,4 ntpd on | ||
+ | </cli> | ||
+ | |||
+ | Add or delete a service | ||
+ | <cli prompt='#'> | ||
+ | # chkconfig add rhnsd | ||
+ | # chkconfig del rhnsd | ||
</cli> | </cli> |