Custom services controlling
Systemd Overview
Getting ready
$ vagrant up
$ vagrant ssh
How to do it
From the man pages systemd, it systemd system and service manager. use helping command for systemctl
# systemctl --help
systemctl key option for basic
$ systemctl --help
systemctl [OPTIONS...] {COMMAND} ...
Query or send control commands to the systemd manager.
-h --help Show this help
--version Show package version
--system Connect to system manager
-H --host=[USER@]HOST
Operate on remote host
-M --machine=CONTAINER
Operate on local container
-t --type=TYPE List units of a particular type
--state=STATE List units with particular LOAD or SUB or ACTIVE state
-p --property=NAME Show only properties by this name
-a --all Show all loaded units/properties, including dead/empty
ones. To list all units installed on the system, use
the 'list-unit-files' command instead.
-l --full Don't ellipsize unit names on output
-r --recursive Show unit list of host and local containers
--reverse Show reverse dependencies with 'list-dependencies'
--job-mode=MODE Specify how to deal with already queued jobs, when
queueing a new job
--show-types When showing sockets, explicitly show their type
-i --ignore-inhibitors
When shutting down or sleeping, ignore inhibitors
--kill-who=WHO Who to send signal to
-s --signal=SIGNAL Which signal to send
--now Start or stop unit in addition to enabling or disabling it
-q --quiet Suppress output
--no-block Do not wait until operation finished
--no-wall Don't send wall message before halt/power-off/reboot
--no-reload Don't reload daemon after en-/dis-abling unit files
--no-legend Do not print a legend (column headers and hints)
--no-pager Do not pipe output into a pager
--no-ask-password
Do not ask for system passwords
--global Enable/disable unit files globally
--runtime Enable unit files only temporarily until next reboot
-f --force When enabling unit files, override existing symlinks
When shutting down, execute action immediately
--preset-mode= Apply only enable, only disable, or all presets
--root=PATH Enable unit files in the specified root directory
-n --lines=INTEGER Number of journal entries to show
-o --output=STRING Change journal output mode (short, short-iso,
short-precise, short-monotonic, verbose,
export, json, json-pretty, json-sse, cat)
--plain Print unit dependencies as a list instead of a tree
Unit Commands:
list-units [PATTERN...] List loaded units
list-sockets [PATTERN...] List loaded sockets ordered by address
list-timers [PATTERN...] List loaded timers ordered by next elapse
start NAME... Start (activate) one or more units
stop NAME... Stop (deactivate) one or more units
reload NAME... Reload one or more units
restart NAME... Start or restart one or more units
try-restart NAME... Restart one or more units if active
reload-or-restart NAME... Reload one or more units if possible,
otherwise start or restart
reload-or-try-restart NAME... Reload one or more units if possible,
otherwise restart if active
isolate NAME Start one unit and stop all others
kill NAME... Send signal to processes of a unit
is-active PATTERN... Check whether units are active
is-failed PATTERN... Check whether units are failed
status [PATTERN...|PID...] Show runtime status of one or more units
show [PATTERN...|JOB...] Show properties of one or more
units/jobs or the manager
cat PATTERN... Show files and drop-ins of one or more units
set-property NAME ASSIGNMENT... Sets one or more properties of a unit
help PATTERN...|PID... Show manual for one or more units
reset-failed [PATTERN...] Reset failed state for all, one, or more
units
list-dependencies [NAME] Recursively show units which are required
or wanted by this unit or by which this
unit is required or wanted
Unit File Commands:
list-unit-files [PATTERN...] List installed unit files
enable NAME... Enable one or more unit files
disable NAME... Disable one or more unit files
reenable NAME... Reenable one or more unit files
preset NAME... Enable/disable one or more unit files
based on preset configuration
preset-all Enable/disable all unit files based on
preset configuration
is-enabled NAME... Check whether unit files are enabled
mask NAME... Mask one or more units
unmask NAME... Unmask one or more units
link PATH... Link one or more units files into
the search path
add-wants TARGET NAME... Add 'Wants' dependency for the target
on specified one or more units
add-requires TARGET NAME... Add 'Requires' dependency for the target
on specified one or more units
edit NAME... Edit one or more unit files
get-default Get the name of the default target
set-default NAME Set the default target
Machine Commands:
list-machines [PATTERN...] List local containers and host
Job Commands:
list-jobs [PATTERN...] List jobs
cancel [JOB...] Cancel all, one, or more jobs
Snapshot Commands:
snapshot [NAME] Create a snapshot
delete NAME... Remove one or more snapshots
Environment Commands:
show-environment Dump environment
set-environment NAME=VALUE... Set one or more environment variables
unset-environment NAME... Unset one or more environment variables
import-environment [NAME...] Import all or some environment variables
Manager Lifecycle Commands:
daemon-reload Reload systemd manager configuration
daemon-reexec Reexecute systemd manager
System Commands:
is-system-running Check whether system is fully running
default Enter system default mode
rescue Enter system rescue mode
emergency Enter system emergency mode
halt Shut down and halt the system
poweroff Shut down and power-off the system
reboot [ARG] Shut down and reboot the system
kexec Shut down and reboot the system with kexec
exit Request user instance exit
switch-root ROOT [INIT] Change to a different root file system
suspend Suspend the system
hibernate Hibernate the system
hybrid-sleep Hibernate and suspend the system
Now Its type
$ systemctl -t help
Available unit types:
service
socket
busname
target
snapshot
device
mount
automount
swap
timer
path
slice
scope
$ systemctl -l
UNIT
proc-sys-fs-binfmt_misc.automount
sys-devices-pci0000:00-0000:00:03.0-net-enp0s3.device
sys-devices-pci0000:00-0000:00:05.0-sound-card0.device
sys-devices-pci0000:00-0000:00:08.0-net-enp0s8.device
sys-devices-pci0000:00-0000:00:09.0-net-enp0s9.device
sys-devices-pci0000:00-0000:00:0a.0-net-enp0s10.device
sys-devices-pci0000:00-0000:00:0d.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-s
sys-devices-pci0000:00-0000:00:0d.0-ata1-host0-target0:0:0-0:0:0:0-block-sda-s
sys-devices-pci0000:00-0000:00:0d.0-ata1-host0-target0:0:0-0:0:0:0-block-sda.d
sys-devices-pci0000:00-0000:00:0d.0-ata2-host1-target1:0:0-1:0:0:0-block-sdb-s
sys-devices-pci0000:00-0000:00:0d.0-ata2-host1-target1:0:0-1:0:0:0-block-sdb-s
sys-devices-pci0000:00-0000:00:0d.0-ata2-host1-target1:0:0-1:0:0:0-block-sdb.d
sys-devices-platform-serial8250-tty-ttyS0.device
sys-devices-platform-serial8250-tty-ttyS1.device
sys-devices-platform-serial8250-tty-ttyS2.device
sys-devices-platform-serial8250-tty-ttyS3.device
sys-devices-virtual-block-dm\x2d0.device
sys-devices-virtual-block-dm\x2d1.device
sys-devices-virtual-net-Br0.device
sys-module-configfs.device
sys-module-fuse.device
sys-subsystem-net-devices-Br0.device
sys-subsystem-net-devices-enp0s10.device
sys-subsystem-net-devices-enp0s3.device
sys-subsystem-net-devices-enp0s8.device
sys-subsystem-net-devices-enp0s9.device
-.mount
boot.mount
dev-hugepages.mount
dev-mqueue.mount
proc-fs-nfsd.mount
proc-sys-fs-binfmt_misc.mount
run-user-1000-gvfs.mount
run-user-1000.mount
sys-fs-fuse-connections.mount
sys-kernel-config.mount
sys-kernel-debug.mount
tmp.mount
var-lib-nfs-rpc_pipefs.mount
brandbot.path
cups.path
systemd-ask-password-plymouth.path
systemd-ask-password-wall.path
session-1.scope
abrt-ccpp.service
abrt-oops.service
abrt-xorg.service
abrtd.service
accounts-daemon.service
alsa-state.service
atd.service
auditd.service
avahi-daemon.service
blk-availability.service
bluetooth.service
chronyd.service
colord.service
crond.service
cups.service
dbus.service
● dhcpd.service
firewalld.service
gdm.service
gssproxy.service
httpd.service
iscsi-shutdown.service
● kdump.service
kmod-static-nodes.service
libstoragemgmt.service
livesys-late.service
livesys.service
lvm2-lvmetad.service
lvm2-monitor.service
lvm2-pvscan@8:2.service
ModemManager.service
NetworkManager.service
nfs-config.service
nmb.service
packagekit.service
polkit.service
postfix.service
rhel-dmesg.service
rhel-import-state.service
rhel-readonly.service
● rngd.service
rsyslog.service
rtkit-daemon.service
smartd.service
smb.service
sysstat.service
systemd-journal-flush.service
systemd-journald.service
systemd-logind.service
systemd-random-seed.service
systemd-remount-fs.service
systemd-sysctl.service
systemd-tmpfiles-setup-dev.service
systemd-tmpfiles-setup.service
systemd-udev-settle.service
systemd-udev-trigger.service
systemd-udevd.service
systemd-update-utmp.service
systemd-user-sessions.service
systemd-vconsole-setup.service
target.service
tuned.service
udisks2.service
upower.service
vsftpd.service
wpa_supplicant.service
xinetd.service
-.slice
system-getty.slice
system-lvm2\x2dpvscan.slice
system.slice
user-1000.slice
user.slice
avahi-daemon.socket
cups.socket
dbus.socket
dm-event.socket
iscsid.socket
iscsiuio.socket
lvm2-lvmetad.socket
lvm2-lvmpolld.socket
rpcbind.socket
systemd-initctl.socket
systemd-journald.socket
systemd-shutdownd.socket
systemd-udevd-control.socket
systemd-udevd-kernel.socket
tftp.socket
dev-mapper-cl\x2dswap.swap
basic.target
cryptsetup.target
getty.target
graphical.target
local-fs-pre.target
local-fs.target
multi-user.target
network-online.target
network.target
nfs-client.target
nss-user-lookup.target
paths.target
remote-fs-pre.target
remote-fs.target
slices.target
sockets.target
sound.target
swap.target
sysinit.target
timers.target
systemd-tmpfiles-clean.timer
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
154 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
$ sudo systemctl --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-xorg.service loaded active running ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting
accounts-daemon.service loaded active running Accounts Service
alsa-state.service loaded active running Manage Sound Card State (res
atd.service loaded active running Job spooling tools
auditd.service loaded active running Security Auditing Service
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
blk-availability.service loaded active exited Availability of block device
bluetooth.service loaded active running Bluetooth service
chronyd.service loaded active running NTP client/server
colord.service loaded active running Manage, Install and Generate
crond.service loaded active running Command Scheduler
cups.service loaded active running CUPS Printing Service
dbus.service loaded active running D-Bus System Message Bus
● dhcpd.service loaded failed failed DHCPv4 Server Daemon
firewalld.service loaded active running firewalld - dynamic firewall
gdm.service loaded active running GNOME Display Manager
gssproxy.service loaded active running GSSAPI Proxy Daemon
httpd.service loaded active running The Apache HTTP Server
iscsi-shutdown.service loaded active exited Logout off all iSCSI session
● kdump.service loaded failed failed Crash recovery kernel arming
kmod-static-nodes.service loaded active exited Create list of required stat
libstoragemgmt.service loaded active running libstoragemgmt plug-in serve
livesys-late.service loaded active exited SYSV: Late init script for l
livesys.service loaded active exited LSB: Init script for live im
lvm2-lvmetad.service loaded active running LVM2 metadata daemon
lvm2-monitor.service loaded active exited Monitoring of LVM2 mirrors,
lvm2-pvscan@8:2.service loaded active exited LVM2 PV scan on device 8:2
ModemManager.service loaded active running Modem Manager
NetworkManager.service loaded active running Network Manager
nfs-config.service loaded active exited Preprocess NFS configuration
nmb.service loaded active running Samba NMB Daemon
packagekit.service loaded active running PackageKit Daemon
polkit.service loaded active running Authorization Manager
postfix.service loaded active running Postfix Mail Transport Agent
rhel-dmesg.service loaded active exited Dump dmesg to /var/log/dmesg
rhel-import-state.service loaded active exited Import network configuration
rhel-readonly.service loaded active exited Configure read-only root sup
● rngd.service loaded failed failed Hardware RNG Entropy Gathere
rsyslog.service loaded active running System Logging Service
rtkit-daemon.service loaded active running RealtimeKit Scheduling Polic
smartd.service loaded active running Self Monitoring and Reportin
smb.service loaded active running Samba SMB Daemon
sshd.service loaded active running OpenSSH server daemon
sysstat.service loaded active exited Resets System Activity Logs
systemd-journal-flush.service loaded active exited Flush Journal to Persisten
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-random-seed.service loaded active exited Load/Save Random Seed
systemd-remount-fs.service loaded active exited Remount Root and Kernel File
systemd-sysctl.service loaded active exited Apply Kernel Variables
systemd-tmpfiles-setup-dev.service loaded active exited Create Static Device
systemd-tmpfiles-setup.service loaded active exited Create Volatile Files and
systemd-udev-settle.service loaded active exited udev Wait for Complete Devic
systemd-udev-trigger.service loaded active exited udev Coldplug all Devices
systemd-udevd.service loaded active running udev Kernel Device Manager
systemd-update-utmp.service loaded active exited Update UTMP about System Boo
systemd-user-sessions.service loaded active exited Permit User Sessions
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
target.service loaded active exited Restore LIO kernel target co
tuned.service loaded active running Dynamic System Tuning Daemon
udisks2.service loaded active running Disk Manager
upower.service loaded active running Daemon for power management
vsftpd.service loaded active running Vsftpd ftp daemon
wpa_supplicant.service loaded active running WPA Supplicant daemon
xinetd.service loaded active running Xinetd A Powerful Replacemen
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
$ sudo systemctl list-unit-files --type=service | grep httpd
httpd.service enabled
$ sudo systemctl list-unit-files --type=service | grep sshd
anaconda-sshd.service static
sshd-keygen.service static
sshd.service enabled
[email protected] static
$ sudo systemctl --failed --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
● dhcpd.service loaded failed failed DHCPv4 Server Daemon
● kdump.service loaded failed failed Crash recovery kernel arming
● rngd.service loaded failed failed Hardware RNG Entropy Gatherer Daemon
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
$ sudo systemctl list-unit-files --type=service | grep xinetd
xinetd.service enabled
$ sudo systemctl list-unit-files --type=socket | grep sshd
sshd.socket disabled
$ sudo systemctl list-unit-files --type=socket --all
UNIT FILE STATE
avahi-daemon.socket enabled
cups.socket enabled
dbus.socket static
dm-event.socket enabled
iscsid.socket enabled
iscsiuio.socket enabled
lldpad.socket disabled
lvm2-lvmetad.socket enabled
lvm2-lvmpolld.socket enabled
rpcbind.socket enabled
rsyncd.socket disabled
sshd.socket disabled
syslog.socket static
systemd-initctl.socket static
systemd-journald.socket static
systemd-networkd.socket disabled
systemd-shutdownd.socket static
systemd-udevd-control.socket static
systemd-udevd-kernel.socket static
tftp.socket enabled
20 unit files listed.
$ sudo systemctl list-units --type=target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
cryptsetup.target loaded active active Encrypted Volumes
getty.target loaded active active Login Prompts
graphical.target loaded active active Graphical Interface
local-fs-pre.target loaded active active Local File Systems (Pre)
local-fs.target loaded active active Local File Systems
multi-user.target loaded active active Multi-User System
network-online.target loaded active active Network is Online
network.target loaded active active Network
nfs-client.target loaded active active NFS client services
nss-user-lookup.target loaded active active User and Group Name Lookups
paths.target loaded active active Paths
remote-fs-pre.target loaded active active Remote File Systems (Pre)
remote-fs.target loaded active active Remote File Systems
slices.target loaded active active Slices
sockets.target loaded active active Sockets
sound.target loaded active active Sound Card
sysinit.target loaded active active System Initialization
timers.target loaded active active Timers
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
$ sudo systemctl list-unit-files --type=target
UNIT FILE STATE
anaconda.target static
basic.target static
bluetooth.target static
cryptsetup-pre.target static
cryptsetup.target static
ctrl-alt-del.target disabled
default.target enabled
emergency.target static
final.target static
getty.target static
graphical.target enabled
halt.target disabled
hibernate.target static
hybrid-sleep.target static
initrd-fs.target static
initrd-root-fs.target static
initrd-switch-root.target static
initrd.target static
iprutils.target disabled
kexec.target disabled
local-fs-pre.target static
local-fs.target static
machines.target disabled
multi-user.target static
network-online.target static
network-pre.target static
network.target static
nfs-client.target enabled
nss-lookup.target static
nss-user-lookup.target static
paths.target static
poweroff.target disabled
printer.target static
reboot.target disabled
remote-fs-pre.target static
remote-fs.target enabled
rescue.target disabled
rpcbind.target static
runlevel0.target disabled
runlevel1.target disabled
runlevel2.target static
runlevel3.target static
runlevel4.target static
runlevel5.target static
runlevel6.target disabled
shutdown.target static
sigpwr.target static
sleep.target static
slices.target static
smartcard.target static
sockets.target static
sound.target static
spice-vdagentd.target static
suspend.target static
swap.target static
sysinit.target static
system-update.target static
time-sync.target static
timers.target static
umount.target static
vsftpd.target disabled
$ sudo systemctl list-units --type=path
UNIT LOAD ACTIVE SUB DESCRIPTION
brandbot.path loaded active waiting Flexible branding
cups.path loaded active waiting CUPS Printer Service Spool
systemd-ask-password-plymouth.path loaded active waiting Forward Password Reques
systemd-ask-password-wall.path loaded active waiting Forward Password Requests t
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
$ sudo systemctl list-unit-files --type=path
UNIT FILE STATE
brandbot.path disabled
cups.path enabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
$ ps aux | grep systemd
root 1 0.0 0.2 125972 4460 ? Ss 10:35 0:05 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root 475 0.0 0.1 36928 2792 ? Ss 10:35 0:00 /usr/lib/systemd/systemd-journald
root 490 0.0 0.1 46432 1692 ? Ss 10:35 0:00 /usr/lib/systemd/systemd-udevd
dbus 640 0.0 0.1 38652 2264 ? Ssl 10:35 0:02 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root 654 0.0 0.0 26476 1012 ? Ss 10:35 0:00 /usr/lib/systemd/systemd-logind
centos7+ 10239 0.0 0.0 112652 972 pts/1 R+ 14:19 0:00 grep --color=auto systemd
$ ps aux | grep xinetd
root 1194 0.0 0.0 29304 92 ? Ss 10:35 0:00 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid
centos7+ 10963 0.0 0.0 112652 972 pts/1 R+ 14:28 0:00 grep --color=auto xinetd
$ ps aux | grep initd
centos7+ 10384 0.0 0.0 112648 968 pts/1 R+ 14:24 0:00 grep --color=auto initd
$ ps aux | grep httpd
root 1202 0.0 0.0 222032 504 ? Ss 10:35 0:00 /usr/sbin/http -DFOREGROUND
apache 2446 0.0 0.0 224116 36 ? S 10:35 0:00 /usr/sbin/http -DFOREGROUND
apache 2447 0.0 0.0 224116 36 ? S 10:35 0:00 /usr/sbin/http -DFOREGROUND
apache 2448 0.0 0.0 224116 36 ? S 10:35 0:00 /usr/sbin/http -DFOREGROUND
apache 2449 0.0 0.0 224116 36 ? S 10:35 0:00 /usr/sbin/http -DFOREGROUND
apache 2450 0.0 0.0 224116 148 ? S 10:35 0:00 /usr/sbin/http -DFOREGROUND
centos7+ 10405 0.0 0.0 112648 968 pts/1 R+ 14:26 0:00 grep --color=auto httpd
$ ps aux | grep sshd
root 10588 1.0 0.2 82552 3600 ? Ss 14:27 0:00 /usr/sbin/sshd -D
centos7+ 10673 0.0 0.0 112648 964 pts/1 R+ 14:27 0:00 grep --color=auto sshd
For the service state
$ sudo systemctl enable sshd.service
$ sudo systemctl start sshd.service
$ sudo systemctl status sshd -l
sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
Active: active (running) since Thu 2016-05-26 06:28:52 EDT; 2min 0s ago
Main PID: 1172 (sshd)
CGroup: /system.slice/sshd.service
└─1172 /usr/sbin/sshd -D
May 26 06:28:52 localhost.localdomain systemd[1]: Started OpenSSH server daemon.
May 26 06:28:52 localhost.localdomain sshd[1172]: Server listening on 0.0.0.0...
May 26 06:28:52 localhost.localdomain sshd[1172]: Server listening on :: port...
May 26 06:28:53 localhost.localdomain sshd[1650]: Accepted publickey for vagr...
May 26 06:29:10 localhost.localdomain sshd[2236]: Accepted publickey for vagr...
Hint: Some lines were ellipsized, use -l to show in full.
$ sudo systemctl stop sshd.service
$ sudo systemctl reload sshd.service
$ ps -p <PID>
$ ps -p 10588
check for knowledge check man pages
systemctl,systemd,systemd.unit,systemd.service,systemd.socket.
Dependencies Unit
$ sudo systemctl stop cups.service
Warning: Stopping cups.service, but it can still be activated by:
cups.socket
cups.path
mask network service
$ sudo systemctl mask network
Created symlink from /etc/systemd/system/network.service to /dev/null.
$ sudo systemctl unmask network
Removed symlink /etc/systemd/system/network.service.
Now Finally how we create custom service
$ sudo systemctl daemon-reload
vim /usr/lib/systemd/system/myservice.service
[Unit]
Description= myservice own Service
After=network.target
[Service]
Type=simple
User=myservice
ExecStart=/usr/bin/myservice start
ExecStop=/usr/bin/myservice1 stop
[Install]
WantedBy=multi-user.target