Search This Blog


Service Conrol With Systemd

Overview and Main Differences

Runlevels are now target units
SysVinit scripts are now service units
PID 1 is now /usr/lib/systemd/systemd

In Ubuntu 15.04 ubuntu's upstart will likely pick up systemd as well leaving all main distros on systemd

The fact that all processes in systemd are started by a service unit are tagged by cgroups making it easier to ensure all related processes are shut down upon service shutdown.

cgroups - provide a way in which related tasks or processes are aggregated or partitioned for ease of management

Service units can be activated on events other than just system startup. They can be activated upon hardware detection and not just the action of switching runlevels in traditional sysvinit.

Resource management is easier with cgroups as well so you can limit easier on a per-process basis

Service units are also started in parallel - or as well as they can be in parallel

Startup Timing and Analysis

Basic analysis of bootup time

Startup finished in 3.742s (kernel) + 1.743s (initrd) + 1min 39.173s (userspace) = 1min 44.658s

More detailed analysis of individual service startup

systemd-analyze blame
1min 35.335s network.service
1.869s postfix.service
1.521s systemd-udev-settle.service

Targets Rather Than Runlevels

Targets in SystemdRunlevels with init

No longer set default init through inittab. The following commands will modify some symbolic links in order to change the method in which it boots up.
Check the default runlevel

systemctl get-default

Set the default runlevel

systemctl set-default

Changing targets

systemctl isolate

Service Units Instead of SysVinit Scripts

Service scripts all exist under /lib/systemd/system/ and are linked to /etc/systemd/system/ directory when they are made active for a specific runlevel

ls -l /etc/systemd/system/
total 8
drwxr-xr-x. 2 root root 54 Apr 29 10:23
lrwxrwxrwx. 1 root root 41 Apr 29 10:23 dbus-org.fedoraproject.FirewallD1.service -> /usr/lib/systemd/system/firewalld.service
lrwxrwxrwx. 1 root root 44 Apr 29 10:22 dbus-org.freedesktop.Avahi.service -> /usr/lib/systemd/system/avahi-daemon.service
lrwxrwxrwx. 1 root root 46 Apr 29 10:22 dbus-org.freedesktop.NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx. 1 root root 57 Apr 29 10:22 dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
lrwxrwxrwx. 1 root root 37 Apr 29 10:27 -> /lib/systemd/system/
drwxr-xr-x. 2 root root 85 Apr 29 10:22
drwxr-xr-x. 2 root root 31 Apr 29 10:22
drwxr-xr-x. 2 root root 4096 Apr 29 10:31
drwxr-xr-x. 2 root root 29 Apr 29 10:23
drwxr-xr-x. 2 root root 4096 Apr 29 10:23
drwxr-xr-x. 2 root root 65 Apr 29 10:23
drwxr-xr-x. 2 root root 43 Apr 29 10:22

Simple Service File


ExecStart=/usr/bin/printf "Hello systemd"

# If you wanted you could include the [HotPlug] directive to startup once there has been an action
# Instead of starting upon initialization of runlevel

Examples of systemd unit files are located man systemd.unit

Some Useful Systemd commands

Masking to prevent from being enabled on startup. It also prevents accidental starting of the service by telling the user that it is masked.

systemctl mask atd
systemctl unmask atd

Mask basically does a symlink to /dev/null as follows

ln -s '/dev/null' '/etc/systemd/system/atd.service'

Showing status of some services which gives further detail on the system unit file

systemctl show sshd

Shows you all of the available unit files with their descriptions

systemctl list-units
systemctl list-unit-files

Show the status of a service startup

journalctl -u $serviceName

Starting a Service Upon an Action

cat /lib/systemd/system/pluralsight.path
Description="Check for crash file"

Enable the service

systemctl enable pluralsight.path
systemctl start pluralsight.path

If the file in the path exists then the service "pluralsight.service" will start

Exploring more commands

Change both the persistent hostname in /etc/hostname and the transient name in the "hostname" command

hostnamectl set-hostname

Change the date and time using one command and not worrying abotu hardware clock and system time at the same time



loginctl session-status 1
1 - root (0)
Since: Sun 2015-05-03 08:32:02 EDT; 36min ago
Leader: 1421 (login)
TTY: ttyS0
Service: login; type tty; class user
State: active
Unit: session-1.scope
ââ1421 login -- root
ââ2371 -bash
ââ2552 loginctl session-status 1
ââ2553 less

loginctl show-user root
Timestamp=Sun 2015-05-03 08:32:02 EDT

Centralized logging mechanism


Look at all of the logs relating to a specific service unit

journalctl -u $serviceUnit

you can configure maximum size of journals using the journal configuration files

#SystemMaxUse= - filesize
#RuntimeMaxUse= - memory size

Journal for this boot

journalctl --this-boot

No comments:

Post a Comment