====== Manage services with systemd ======
Systemd is now used on most Linux distribution: Redhat, Suse, Ubuntu...
By default on opensuse 12.3, NetworkManager doesn't automatically starts
==== List all services ====
List running units:
$ systemctl
or:
$ systemctl list-units
List failed units:
$ systemctl --failed
## List all services
$ systemctl list-units --type=service
$ systemctl list-units --type mount
## List running services
$ systemctl list-units --type=service --state=running
## List all svc in simplified way
$ service --status-all
The available unit files can be seen in /usr/lib/systemd/system/ and /etc/systemd/system/ (the latter takes precedence). You can see a list of the installed unit files with:
$ systemctl list-unit-files
graphite:~ # systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
.....
apache2.service disabled
atd.service disabled
autofs.service disabled
autovt@.service disabled
autoyast-initscripts.service disabled
avahi-daemon.service enabled
graphite:~ # systemctl --all
UNIT LOAD ACTIVE SUB JOB DESCRIPTION
cups.path loaded active waiting CUPS Printer Service Spool
systemd-ask-password-console.path loaded inactive dead Dispatch Password Requests to Console Directory Watch
systemd-ask-password-plymouth.path loaded active waiting Forward Password Requests to Plymouth Directory Watch
systemd-ask-password-wall.path loaded active waiting Forward Password Requests to Wall Directory Watch
acpid.service error inactive dead acpid.service
alsa-restore.service loaded inactive dead Restore Sound Card State
alsa-store.service loaded inactive dead Store Sound Card State
amavis.service error inactive dead amavis.service
apparmor.service error inactive dead apparmor.service
auditd.service error inactive dead auditd.service
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
manu@manu-pc:~> systemctl list-dependencies default.target
default.target
● ├─display-manager.service
● ├─langset.service
● ├─systemd-readahead-collect.service
● ├─systemd-readahead-replay.service
● ├─systemd-update-utmp-runlevel.service
● ├─vboxadd.service
● ├─vboxdrv.service
● ├─YaST2-Firstboot.service
● ├─YaST2-Second-Stage.service
● └─multi-user.target
● ├─after-local.service
● ├─apache2.service
● ├─auditd.service
● ├─avahi-daemon.service
● ├─cron.service
● ├─cups.service
● ├─dbus.service
● ├─irqbalance.service
==== To enable NetworkManager, use: ====
=== First, stop the running service: ===
$ systemctl is-active network.service && systemctl stop network.service
=== Enable the NetworkManager service: ===
systemctl --force enable NetworkManager.service
graphite:~ # systemctl --force enable NetworkManager.service
ln -s '/usr/lib/systemd/system/NetworkManager.service' '/etc/systemd/system/network.service'
ln -s '/usr/lib/systemd/system/NetworkManager.service' '/etc/systemd/system/multi-user.target.wants/NetworkManager.service'
ln -s '/usr/lib/systemd/system/NetworkManager-wait-online.service' '/etc/systemd/system/network.target.wants/NetworkManager-wait-online.service'
Check whether a service is already enabled or not:
$ systemctl is-enabled foo.service; echo $?
0 indicates that it is enabled. 1 indicates that it is disabled
=== Start the NetworkManager service (via alias link): ===
$ systemctl start network.service
==== To disable NetworkManager, use: ====
Stop the running service:
$ systemctl is-active network.service && \
systemctl stop network.service
Disable the NetworkManager service:
$ systemctl disable NetworkManager.service
Start the /etc/init.d/network service:
$ systemctl start network.service
all active and failed services:
$ systemctl list-units --type service --state running,failed
==== To query the currently selected service, use: ====
graphite:~ # systemctl status network.service
NetworkManager.service - Network Manager
Loaded: loaded (/usr/lib/systemd/system/NetworkManager.service; enabled)
Active: inactive (dead)
CGroup: name=systemd:/system/NetworkManager.service
==== Start / Stop a service ====
# sytemctl stop network.service
# sytemctl start network.service
# sytemctl status network.service
==== Create a new service ====
First create 2 files:
the script: /usr/lib/systemd/scripts/vgaoff
the service: /usr/lib/systemd/system/vgaoff.service
Now you have to edit the service file. Its content depends on how your script works:
If vgaoff just powers off the gpu, e.g.:
exec blah-blah pwrOFF etc
then the content of vgaoff.service should be:
[Unit]
Description=Power-off gpu
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/vgaoff
[Install]
WantedBy=multi-user.target
If vgaoff is used to power off the GPU and also to power it back on, e.g.:
start() {
exec blah-blah pwrOFF etc
}
stop() {
exec blah-blah pwrON etc
}
case $1 in
start|stop) "$1" ;;
esac
then the content of vgaoff.service should be:
[Unit]
Description=Power-off gpu
[Service]
Type=oneshot
ExecStart=/usr/lib/systemd/scripts/vgaoff start
ExecStop=/usr/lib/systemd/scripts/vgaoff stop
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
chmod 644 /usr/lib/systemd/system/vgaoff.service
Once you have the files in place and configured you can enable the service:
systemctl enable vgaoff.service
It should then start automatically after rebooting the machine.
List service dependancy
[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
Add a dependancy to start after network and local filesystem mount
[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
==== Change default target to boot (runlevel) ====
The standard target is default.target, which is aliased by default to graphical.target (which roughly corresponds to the old runlevel 5). To change the default target at boot-time, append one of the following kernel parameters to your bootloader:
systemd.unit=multi-user.target (which roughly corresponds to the old runlevel 3),
systemd.unit=rescue.target (which roughly corresponds to the old runlevel 1).
Check the current runlevel:
manu@manu-pc:~> systemctl get-default
graphical.target
Alternatively, you may leave the bootloader alone and change default.target. This can be done using systemctl:
# systemctl set-default multi-user.target
To be able to override the previously set default.target, use the force option:
# systemctl set-default -f multi-user.target
The effect of this command is output by systemctl; a symlink to the new default target is made at /etc/systemd/system/default.target.
//For information//
# ls -l /usr/lib/systemd/system/run* | grep -v wants
lrwxrwxrwx 1 root root 15 May 1 17:34 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx 1 root root 13 May 1 17:34 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx 1 root root 17 May 1 17:34 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx 1 root root 17 May 1 17:34 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx 1 root root 17 May 1 17:34 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx 1 root root 16 May 1 17:34 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx 1 root root 13 May 1 17:34 /usr/lib/systemd/system/runlevel6.target -> reboot.target
==== Systemd debug mode ====
Add the following lines to /etc/systemd/system.conf
LogLevel=debug
LogTarget=syslog-or-kmsg
SysVConsole=yes
Output will be printed to console.
==== Systemd logging ====
A new service
Show all boot messages:
# journalctl -b : Show all messages from this boot
# journalctl -b -0 : shows messages from the current boot
# journalctl -b -x : shows messages from the last x boot time
Show all messages from date (and optional time):
# journalctl --since="2012-10-30 18:17:16"
Show all messages since 20 minutes ago:
# journalctl --since "20 min ago"
Follow new messages (like: tail -f /var/log/messages):
# journalctl -f
List only errors:
# journalctl -p err
List detailled:
# journalctl -o verbose
Show all messages by a specific executable:
# journalctl /usr/lib/systemd/systemd
==== Systemd analyse ====
# systemd-analyze
Startup finished in 884ms (kernel) + 3.861s (initrd) + 48.356s (userspace) = 53.102s
# systemd-analyze blame
Find Out Time Each Unit Took to Start
16.159s mariadb.service
12.178s libvirtd.service
10.298s tuned.service
9.836s postfix.service
8.704s lsws.service
7.352s lscpd.service
4.988s mysqld@1.service
4.779s NetworkManager-wait-online.service
4.577s lvm2-monitor.service
4.439s ModemManager.service
# systemd-analyze critical-chain
multi-user.target @48.342s
└─mariadb.service @31.560s +16.159s
└─network.target @31.558s
└─network.service @30.819s +738ms
Realtime stats for services
[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 - -
...
Journal config file (2 ways to list it)
[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
====== 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
[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
Status of a service
# service sshd status
# service --status-all
enable or disable
# chkconfig httpd off
# chkconfig ntpd on
enable for specific level
# chkconfig --level 3,4 ntpd on
Add or delete a service
# chkconfig add rhnsd
# chkconfig del rhnsd