Search This Blog

2015-05-03

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

systemd-analyze
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
:
SNIP

Targets Rather Than Runlevels


Targets in SystemdRunlevels with init
poweroff.target0
rescue.target1
multiuser.target2
multiuser.target3
multiuser.target4
graphical.target5
reboot.target6

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 multi-user.target


Changing targets

systemctl isolate multi-user.target

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 basic.target.wants
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 default.target -> /lib/systemd/system/multi-user.target
drwxr-xr-x. 2 root root 85 Apr 29 10:22 default.target.wants
drwxr-xr-x. 2 root root 31 Apr 29 10:22 getty.target.wants
drwxr-xr-x. 2 root root 4096 Apr 29 10:31 multi-user.target.wants
drwxr-xr-x. 2 root root 29 Apr 29 10:23 nfs.target.wants
drwxr-xr-x. 2 root root 4096 Apr 29 10:23 sockets.target.wants
drwxr-xr-x. 2 root root 65 Apr 29 10:23 sysinit.target.wants
drwxr-xr-x. 2 root root 43 Apr 29 10:22 system-update.target.wants

Simple Service File

[Unit]
Description=MyService
After=sshd.service

[Service]
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 multi-user.target runlevel
[Install]
WantedBy=multi-user.target


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
[Unit]
Description="Check for crash file"
After=sshd.service
[Path]
PathExists=/tmp/app.crash
Unit=pluralsight.service
[Install]
WantedBy=multi-user.target


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
hostnamectl set-hostname


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

timedatectl


localectl


loginctl
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
UID=0
GID=0
Name=root
Timestamp=Sun 2015-05-03 08:32:02 EDT
TimestampMonotonic=76471883
RuntimePath=/run/user/0
Slice=user-0.slice
State=active
Sessions=1
IdleHint=no
IdleSinceHint=1430658536092431
IdleSinceHintMonotonic=2290181276


Centralized logging mechanism

journalctl


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

/etc/systemd/journald.conf
#SystemMaxUse= - filesize
#RuntimeMaxUse= - memory size


Journal for this boot

journalctl --this-boot


No comments:

Post a Comment