Systemd – Services

Command demonstrated in this article is tested on RHEL 7 but it can work on CentOS / Fedora / Amazon Linux / Ubuntu and other linux distro as well. This is a second part of series of post that I would be covering entire Systemd and its sub-systems.

Part 1 What is Systemd
Part 2 Systemd – Services (Current post)

Making System Daemon to Start and Stop at run time
Starting Service

# systemctl start sshd
OR
# systemctl start sshd.service


Note: Both the above command are same and it is optional to put .service.
Verify service is started

# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Tue 2017-08-01 15:49:15 IST; 5s ago
  Process: 2153 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
 Main PID: 2160 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service
           ├─2160 /usr/sbin/httpd -DFOREGROUND
           ├─2161 /usr/sbin/httpd -DFOREGROUND
           ├─2162 /usr/sbin/httpd -DFOREGROUND
           ├─2163 /usr/sbin/httpd -DFOREGROUND
           ├─2164 /usr/sbin/httpd -DFOREGROUND
           └─2165 /usr/sbin/httpd -DFOREGROUND

Aug 01 15:49:15 server.example.com systemd[1]: Starting The Apache HTTP Server...
Aug 01 15:49:15 server.example.com systemd[1]: Started The Apache HTTP Server.

Stopping Service

# systemctl stop httpd


Verify service is stopped

# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: inactive (dead) since Tue 2017-08-01 15:51:30 IST; 31s ago
  Process: 2230 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 2160 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
 Main PID: 2160 (code=exited, status=0/SUCCESS)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"

Aug 01 15:49:15 server.example.com systemd[1]: Starting The Apache HTTP Server...
Aug 01 15:49:15 server.example.com systemd[1]: Started The Apache HTTP Server.
Aug 01 15:51:29 server.example.com systemd[1]: Stopping The Apache HTTP Server...
Aug 01 15:51:30 server.example.com systemd[1]: Stopped The Apache HTTP Server.

Verify service is activated or not

# systemctl is-active httpd
inactive


Re-starting and Verifying Service

# systemctl restart httpd
# systemctl is-active httpd
active

Reloading Configuration file of a Service

# systemctl reload httpd
# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
   Active: active (running) since Tue 2017-08-01 15:53:31 IST; 35s ago
  Process: 2284 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 2297 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
 Main PID: 2288 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─2288 /usr/sbin/httpd -DFOREGROUND
           ├─2299 /usr/sbin/httpd -DFOREGROUND
           ├─2300 /usr/sbin/httpd -DFOREGROUND
           ├─2301 /usr/sbin/httpd -DFOREGROUND
           ├─2302 /usr/sbin/httpd -DFOREGROUND
           └─2303 /usr/sbin/httpd -DFOREGROUND

Aug 01 15:53:31 server.example.com systemd[1]: Starting The Apache HTTP Server...
Aug 01 15:53:31 server.example.com systemd[1]: Started The Apache HTTP Server.
Aug 01 15:54:06 server.example.com systemd[1]: Reloading The Apache HTTP Server.
Aug 01 15:54:06 server.example.com systemd[1]: Reloaded The Apache HTTP Server.


Making System Daemon to Start and Stop at boot time
Enabling Service

# systemctl enable sshd
ln -s '/usr/lib/systemd/system/sshd.service' '/etc/systemd/system/multi-user.target.wants/sshd.service'

Note: Enabling service is creating a symbolic link in /etc/systemd/system/<Target-Run-Level.wants>, but the actual script is located inside /usr/lib/systemd/system.

Verify service is enabled at startup

# systemctl is-enabled sshd
enabled


Disabling Service

# systemctl disable sshd
rm '/etc/systemd/system/multi-user.target.wants/sshd.service'

Verify service is enabled at startup

# systemctl status sshd
sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; disabled)
Active: inactive (dead)


Note: Disabling service does not stop the service.

Masking service

Sometime system may have conflicting services installed. For example firewalls can be started by “iptables” and “firewalld” service, also in case of networks service it can be started using “network” and “Network Manager” service. So to prevent an administrator from accidentally starting a service, it can be masked. Masking will create a link in the configuration directories so that if the service is started, nothing will happen.

# systemctl mask network
ln -s '/dev/null' '/etc/systemd/system/network.service'

To Un-Mask Service

# systemctl unmask network
rm '/etc/systemd/system/network.service'

Different States of Service

Command systemct1 status name.type is used to view the status of service. If the unit type is not provided, systemct1 will show the status of a all service unit, if one exists.
Try # systemct1 status command at command prompt
Below output illustrated status (running) of firewalld Service.

# systemctl status firewalld
firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
Active: active (running) since Sun 2017-07-30 21:47:34 IST; 1h 27min ago
 Main PID: 624 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─624 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Jul 30 21:47:34 server.example.com systemd[1]: Started firewalld - dynamic firewall daemon.


Below table list down various Keywords that indicate service status :-

Keyword Description
loaded Unit configuration file has been processed.
Active(running) Running with one or more continuing processes.
active (exited) Successfully completed a one-time configuration.
active (waiting) Running but waiting for event.
inactive not running.
enabled Will be started at boot time.
disabled Will not be started at boot time.
static Can not be enabled, but may be started by an enabled unit automatically

Below output illustrated status (exited) of NFS Server Service.

# systemctl status nfs-server
nfs-server.service - NFS Server
Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; enabled)
Active: active (exited) since Sun 2017-07-30 21:48:28 IST; 1h 34min ago
  Process: 1784 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS $RPCNFSDCOUNT (code=exited, status=0/SUCCESS)
  Process: 1780 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
  Process: 1779 ExecStartPre=/usr/libexec/nfs-utils/scripts/nfs-server.preconfig (code=exited, status=0/SUCCESS)
 Main PID: 1784 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service

Jul 30 21:48:28 server.example.com systemd[1]: Starting NFS Server...
Jul 30 21:48:28 server.example.com systemd[1]: Started NFS Server.

Below output illustrated status “static and inactive (dead)” of TMP CleanUp Service Service.

# systemctl status systemd-tmpfiles-clean
systemd-tmpfiles-clean.service - Cleanup of Temporary Directories
Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.service; static)
   Active: inactive (dead) since Sun 2017-07-30 22:02:17 IST; 1h 28min ago
     Docs: man:tmpfiles.d(5)
           man:systemd-tmpfiles(8)
  Process: 2072 ExecStart=/usr/bin/systemd-tmpfiles --clean (code=exited, status=0/SUCCESS)
 Main PID: 2072 (code=exited, status=0/SUCCESS)

Jul 30 22:02:17 server.example.com systemd[1]: Starting Cleanup of Temporary Directories...
Jul 30 22:02:17 server.example.com systemd[1]: Started Cleanup of Temporary Directories.

Unit dependencies

Services  may be started as  dependencies of other services.  If a socket unit  is enabled and the  service unit with the  same name is not,  the service will automatically  be started when a request is made on  the network socket. Services  may also be triggered  by path units when  a file system  condition is met. For  example, a file placed  into the print spool  directory will cause the  cups print service to  be started if it  is not running.

The  system1 list-dependencies

UNIT  command can be used  to print out a  tree of what other  units must be started if the specified unit  is started. Depending on  the exact dependency. the  other unit may need  to be running before  or after the specified unit starts.

# systemctl list-dependencies sshd
sshd.service
├─system.slice
└─basic.target
  ├─alsa-restore.service
  ├─alsa-state.service
  ├─firewalld.service
  ├─microcode.service
  ├─rhel-autorelabel-mark.service
  ├─rhel-autorelabel.service
  ├─rhel-configure.service
  ├─rhel-dmesg.service
  ├─rhel-loadmodules.service
  ├─paths.target
  ├─slices.target
  │ ├─-.slice
  │ └─system.slice
  ├─sockets.target
  │ ├─avahi-daemon.socket
  │ ├─dbus.socket
  │ ├─dm-event.socket
  │ ├─iscsid.socket
  │ ├─iscsiuio.socket
  │ ├─lvm2-lvmetad.socket
  │ ├─rpcbind.socket
  │ ├─systemd-initctl.socket
  │ ├─systemd-journald.socket
  │ ├─systemd-shutdownd.socket
  │ ├─systemd-udevd-control.socket
  │ └─systemd-udevd-kernel.socket
............

Listing active state of all loaded unit

# systemctl list-units --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 Tool
accounts-daemon.service        loaded active running Accounts Service
...........

Listing active/inactive state of all loaded unit

# systemctl list-units --type=service --all
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-vmcore.service                loaded inactive dead    Harvest vmcores for ABRT
abrt-xorg.service                  loaded active   running ABRT Xorg log watcher
accounts-daemon.service            loaded active   running Accounts Service
alsa-restore.service               loaded inactive dead    Restore Sound Card State
alsa-store.service                 loaded inactive dead    Store Sound Card State
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
brandbot.service                   loaded inactive dead    Flexible Branding Service
...........

Listing enable/diaable state of all loaded unit

# systemctl list-unit-files --type=service 
UNIT FILE                                   STATE   
abrt-ccpp.service                           enabled 
abrt-oops.service                           enabled 
abrt-pstoreoops.service                     disabled
abrt-vmcore.service                         enabled 
abrt-xorg.service                           enabled 
abrtd.service                               enabled 
accounts-daemon.service                     enabled 
alsa-restore.service                        static  
alsa-state.service                          static  
alsa-store.service                          static  
anaconda-direct.service                     static  
anaconda-noshell.service                    static  
anaconda-shell@.service                     static  
anaconda-sshd.service                       static  
anaconda-tmux@.service                      static  
anaconda.service                            static  
arp-ethers.service                          disabled
...........

Listing failed services

# systemctl --failed --type=service
UNIT                    LOAD   ACTIVE SUB    DESCRIPTION
rhnsd.service           loaded failed failed LSB: Starts the Spacewalk Daemon
rngd.service            loaded failed failed Hardware RNG Entropy Gatherer Daemon
vboxadd-service.service loaded failed failed vboxadd-service.service
vboxadd.service         loaded failed failed vboxadd.service

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.

4 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

Below table list out the comparative commands between old sysVinit and new systemctl features that would really help in day to day operation.

sysVinit command systemd command Comment
service sshd start systemctl start sshd.service Used to start a service (not reboot persistent)
service sshd stop systemctl stop sshd Used to stop a service. (not reboot persistent)
service sshd restart systemctl restart sshd Used to start and stop a service.
service sshd reload systemctl reload sshd When supported, reloads the config file without interrupting pending operations.
service sshd condrestart systemctl condrestart sshd Restarts if the service is already running.
service sshd status systemctl status sshd Tells whether a service is currently running.
ls /etc/rc.d/init.d/ systemctl list-unit-files –type=service Lists all available services.
chkconfig sshd on systemctl enable sshd Always run the service at this target (runlevel.)
chkconfig sshd off systemctl disable sshd Do not automatically run the service at this target (runlevel.)
chkconfig –list systemctl list-units -t service –all Print a table of available services and their status.
chkconfig sshd –list ls /etc/systemd/system/*.wants/sshd.service Lists the targets that will include the service.
chkconfig sshd –add systemctl daemon-reload Used when you create a service file or modify any configuration.
Advertisements

One thought on “Systemd – Services

  1. Pingback: Systemd

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s