LINUX-26
SERVICE MANAGEMENT FRAMEWORK (RHEL-7)
SYSTEMD/SYSTEMCTL
– PART-3
We already covered about management of
services, targets with systemd in previous posts.
# systemctl start [name of service]
# systemctl stop [name of service]
# systemctl restart [name of service]
# systemctl reload [name of service]
# systemctl status [name of service]
# systemctl mask [name of service]
# systemctl unmask [name of service]
# systemctl is-active [name of service]
# systemctl list-units --type service –all
So almost we covered all necessary areas in
previous posts, then what we are going to have now…?
Nothing much, just analysis of unit files
and few more interesting things…
[root@rhel7-client ~]# cat
/usr/lib/systemd/system/rsyslog.service
[Unit]
Description=System Logging Service
;Requires=syslog.socket
[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/rsyslog
ExecStart=/usr/sbin/rsyslogd -n
$SYSLOGD_OPTIONS
StandardOutput=null
[Install]
WantedBy=multi-user.target
;Alias=syslog.service
==========================================================================
[root@rhel7-client ~]# cat
/usr/lib/systemd/system/iscsi.service
[Unit]
Description=Login and scanning of iSCSI
devices
Documentation=man:iscsid(8) man:iscsiadm(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-remount-fs.service
network.target iscsid.service iscsiuio.service
Before=remote-fs-pre.target
ConditionDirectoryNotEmpty=|/var/lib/iscsi/nodes
ConditionDirectoryNotEmpty=|/sys/class/iscsi_session
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/libexec/iscsi-mark-root-nodes
SuccessExitStatus=21
ExecStart=/sbin/iscsiadm -m node
--loginall=automatic
ExecStop=/sbin/iscsiadm -m node --logoutall=automatic
ExecReload=/sbin/iscsiadm -m node
--loginall=automatic
[Install]
WantedBy=sysinit.target
==========================================================================
[root@rhel7-client ~]# cat
/usr/lib/systemd/system/tmp.mount
#
This file is part of systemd.
#
#
systemd is free software; you can redistribute it and/or modify it
#
under the terms of the GNU Lesser General Public License as published by
# the
Free Software Foundation; either version 2.1 of the License, or
# (at
your option) any later version.
[Unit]
Description=Temporary Directory
Documentation=man:hier(7)
Documentation=http://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target
[Mount]
What=tmpfs
Where=/tmp
Type=tmpfs
Options=mode=1777,strictatime
# Make 'systemctl enable tmp.mount' work:
[Install]
WantedBy=local-fs.target
Referred three files for better
understanding,
We have 3 common columns in every file, i.e.
UNIT / SERVICE / INSTALL, MOUNT is additional in tmp.mount file.
UNIT:
OPTIONS
|
DESCRIPTION
|
Description
|
Service function detail
|
Requires
|
Requirement dependencies
on other units. If this unit gets activated, the units listed here will be
activated as well. If one of the other units gets deactivated or its
activation fails, this unit will be deactivated.
|
Documentation
|
Location for docs either
internal man page
|
or external url’s.
|
|
DefaultDependencies
|
Takes a boolean argument.
If true, (the default), a few default dependencies will implicitly be created
for the unit. The actual dependencies created depend on the unit type. For
example, for service units, these dependencies ensure that the service is
started only after basic system initialization is completed and is properly
terminated on system shutdown.
|
Conflicts
|
This can be used to list
units that cannot be run at the same time as the current unit. Starting a
unit with this relationship will cause the other units to be stopped.
|
After
|
The units listed in this
directive will be started before starting the current unit. After units
refers to a unit that needs to be started before this unit.
|
Before
|
The units listed in this
directive will not be started until the current unit is marked as started if
they are activated at the same time. Before relates to units that is started
after this unit.
|
ConditionPathExists
|
A file existence
condition is checked before a unit is started. If the specified absolute path
name does not exist, the condition will fail.
|
ConditionDirectoryNotEmpty
|
Is similar to ConditionPathExists= but
verifies whether a certain path exists and is a non-empty directory.
|
For more details about different available
unit options, refer to
[root@rhel7-client ~]# man
systemd.unit
SERVICE:
Type=simple (default): systemd considers the service
to be started up immediately. The process must not fork. Do not use this type
if other services need to be ordered on this service, unless it is socket
activated.
Type=forking: systemd considers the service started up once the
process forks and the parent has exited. For classic daemons use this type
unless you know that it is not necessary. You should specify PIDFile= as well
so systemd can keep track of the main process.
Type=oneshot: this is useful for scripts that do a single job
and then exit. You may want to set RemainAfterExit=yes as well so that systemd
still considers the service as active after the process has exited.
Type=notify: identical to Type=simple, but with the
stipulation that the daemon will send a signal to systemd when it is ready. The
reference implementation for this notification is provided by
libsystemd-daemon.so.
Type=dbus: the service is considered ready when the
specified BusName appears on DBus's system bus.
Type=idle: systemd will delay execution of the service
binary until all jobs are dispatched. Other than that behavior is very similar
to Type=simple.
OPTIONS
|
DESCRIPTION
|
Type
|
Several different start-up types
|
EnvironmentFile
|
Additional variables may
be configured by the following means: for processes spawned in specific
units.
|
ExecStart
|
Commands with their
arguments that are executed when this service is started. For each of the
specified commands, the first argument must be an absolute and literal path
to an executable.
|
StandardOutput
|
Controls where file
descriptor 1 (STDOUT) of the executed processes is connected to. Takes one of
inherit, null, tty, journal, syslog,kmsg, journal+console, syslog+console,
kmsg+console or socket.
|
RemainAfterExit
|
Takes a boolean value
that specifies whether the service shall be considered active even when all
its processes exited. Defaults to no.
|
SuccessExitStatus
|
Takes a list of exit
status definitions that when returned by the main service process will be
considered successful termination, in addition to the normal successful exit
code 0 and the signals SIGHUP, SIGINT, SIGTERM, and SIGPIPE.
|
ExecStop
|
Commands to execute to
stop the service started via ExecStart
|
ExecReload
|
Commands to execute to
trigger a configuration reload in the service. This argument takes multiple
command lines, following the same scheme as described for ExecStart
|
For more details about different available service
options, refer to
[root@rhel7-client ~]# man systemd.service
[root@rhel7-client ~]# man systemd.exec
INSTALL:
WANTS: systemd defined parameter where, which units
systemd wants when starting a specific target. Wants are created when systemd
units are enabled, by creating a sym link in the /etc/systemd/system directory,
where subdirectories are found for every target containing wants as sym links
to specific services that are to be started.
OPTIONS
|
DESCRIPTION
|
WantedBy
|
directive is the most
common way to specify how a unit should be enabled. This directive allows you
to specify a dependency relationship in a similar way to the Wants= directive
does in the [Unit] section. Can be seen by “systemctl enable
<service>”, it will create a sym link in the wants directory for the
multiuser target to ensure that the service gets back after a restart.
|
Alias
|
Additional units to
install/deinstall when this unit is installed/deinstalled. If the user
requests installation/deinstallation of a unit with this option configured,
systemctl enable and systemctl disable will
automatically install/uninstall units listed in this option as well. |
RequiredBy
|
This directive is very
similar to the WantedBy= directive, but instead specifies a required
dependency that will cause the activation to fail if not met. When enabled, a
unit with this directive will create a directory ending with .requires
|
Also
|
This directive allows
units to be enabled or disabled as a set. Supporting units that should always
be available when this unit is active can be listed here. They will be
managed as a group for installation tasks.
|
DefaultInstance
|
For template units
(covered later) which can produce unit instances with unpredictable names,
this can be used as a fallback value for the name if an appropriate name is
not provided.
|
For more details about different available install
options, refer to
[root@rhel7-client ~]# man systemd.unit
MOUNT:
Mount units are often translated directly from /etc/fstab files
during the boot process.
OPTIONS
|
DESCRIPTION
|
What
|
The absolute path to the resource that needs to be mounted.
|
Where
|
he absolute path of the
mount point where the resource should be mounted. This should be the same as
the unit file name, except using conventional filesystem notation.
|
Type
|
The filesystem type of
the mount.
|
Options
|
Any mount options that
need to be applied. This is a comma-separated list.
|
TimeoutSec
|
Configures the amount of
time the system will wait until the mount operation is marked as failed.
|
SOCKET UNITS:
Socket creates a method for application to communicate with each
other. Some services create their own socket while starting. Whereas other
services need a socket unit file to create sockets for them. Every socket needs
a corresponding service file.
Socket units on the other hand don't actually start daemons on their
own, instead they just sit there and listen on an ip and a port, or a UNIX
domain socket, and when something connects to it the daemon that the socket is
for is started and the connection is handed to it.
So we had seen about different options inside a unit file, isn’t it
great to see all available options in one go for a specific unit…??
[root@rhel7-client ~]# systemctl show crond
DBUS:
D-Bus is a message bus system that provides an easy way for
inter-process communication. It consists of a daemon, which can be run both
system-wide and for each user session, and a set of libraries to allow
applications to use D-Bus.
dbus-monitor --system - To monitor the activities in the system bus.
dbus-monitor --session - To monitor the activities in the session
bus.
dbus-send <PARAMETER> - To send a message. See the dbus-send
man page (man dbus-send) for more information.
In computing, D-Bus or DBus (for "Desktop Bus"), a software
bus, is an inter-process communication (IPC) and remote procedure call (RPC)
mechanism that allows communication between multiple computer programs (that is,
processes) concurrently running on the same machine. D-Bus was developed as
part of the freedesktop.org project, initiated by Havoc Pennington from Red Hat
to standardize services provided by Linux desktop environments such as GNOME
and KDE.
CGROUPS (CONTROL GRUOUPS):
Linux kernel feature to group processes together and control / limit
their resource usage (CPU, memory, disk I/O, network, etc…). It is considered
that when a service is stopped all processes belong to that service should
terminated simultaneously. “cgroups” help to achieve this target.
Systemd labels processes associated with a service using cgroups. And
cgroups helps to kill all processes in group when corresponding service
terminated.
To get the full hierarchy of control groups
[root@rhel7-client ~]# systemd-cgls
To get the list of control group ordered by CPU, memory and disk I/O
load
[root@rhel7-client ~]# systemd-cgtop
SERVICE ACCOUNTING:
[root@rhel7-client ~]# mkdir
/etc/systemd/system/crond.service.d
[root@rhel7-client ~]# vi
/etc/systemd/system/crond.service.d/accounting.conf
[Service]
MemoryAccounting=true
CPUAccounting=true
[root@rhel7-client ~]# systemctl daemon-reload; systemctl
restart crond
[root@rhel7-client ~]# systemd-cgtop |grep -i cron
/system.slice/crond.service 1 -
660.0K - -
No comments:
Post a Comment