Starting and stopping systemd services

Although this recipe uses services by their base name, they can also be addressed by their full filename. For example, sshd can be substituted by sshd.service.

How to do it…

The following steps need to be performed to successfully start or stop systemd services:

  1. List all available systemd services, as follows:
    ~]# systemctl list-unit-files --type service
    UNIT FILE                                   STATE   
    atd.service                                 enabled
    auditd.service                              enabled
    auth-rpcgss-module.service                  static  
    [email protected]                             disabled
    avahi-daemon.service                        disabled
    blk-availability.service                    disabled
    brandbot.service                            static  
    
    ...
    
    systemd-udev-trigger.service                static  
    systemd-udevd.service                       static  
    systemd-update-utmp-runlevel.service        static  
    systemd-update-utmp.service                 static  
    systemd-user-sessions.service               static  
    systemd-vconsole-setup.service              static  
    tcsd.service                                disabled
    [email protected]                              static  
    tuned.service                               enabled
    wpa_supplicant.service                      disabled
    xinetd.service                              enabled
    
    161 unit files listed.
    

    This shows all service units available followed by information regarding whether the service is enabled or not.

  2. Now, list all the loaded systemd services and their status, as follows:
    ~]# systemctl list-units --type service --all
    UNIT                        LOAD   ACTIVE   SUB     DESCRIPTION
    atd.service                 loaded active   running Job spooling tools
    auditd.service              loaded active   running Security Auditing Service
    auth-rpcgss-module.service  loaded inactive dead    Kernel Module supporting RPC
    brandbot.service            loaded inactive dead    Flexible Branding Service
    cpupower.service            loaded inactive dead    Configure CPU power related
    crond.service               loaded active   running Command Scheduler
    cups.service                loaded inactive dead    CUPS Printing Service
    dbus.service                loaded active   running D-Bus System Message Bus
    ...
    
    systemd-...es-setup.service loaded active   exited  Create Volatile Files and Di
    systemd-...-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 Reb
    systemd-...sessions.service loaded active   exited  Permit User Sessions
    systemd-...le-setup.service loaded active   exited  Setup Virtual Console
    tuned.service               loaded active   running Dynamic System Tuning 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.
    
    103 loaded units listed.
    To show all installed unit files use 'systemctl list-unit-files'.
    ~]#
    
  3. Next, get the status of a service.

    To get the status of a particular service, execute the following, substituting <service> with the name of the service:

    ~]# systemctl status <service>
    

    Here's an example:

    ~]# systemctl status sshd
    sshd.service - OpenSSH server daemon
       Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled)
       Active: active (running) since Fri 2015-07-17 09:13:55 CEST; 1 weeks 0 days ago
     Main PID: 11880 (sshd)
       CGroup: /system.slice/sshd.service
               +-11880 /usr/sbin/sshd -D
    
    Jul 22 12:07:31 rhel7.mydomain.lan sshd[10340]: Accepted publickey for root...
    Jul 22 12:12:29 rhel7.mydomain.lan sshd[10459]: Accepted publickey for root...
    Jul 22 12:13:33 rhel7.mydomain.lan sshd[10473]: Accepted publickey for root...
    Jul 24 21:27:24 rhel7.mydomain.lan sshd[28089]: Accepted publickey for root...
    Hint: Some lines were ellipsized, use -l to show in full.
    ~]#
    
  4. Now, start and stop the systemd services.

    To stop a systemd service, execute the following, substituting <service> with the name of the service:

    ~]# systemctl stop <service>
    

    Here's an example:

    ~]# systemctl stop sshd
    

    To start a systemd service, execute the following, substituting <service> with the name of the service:

    ~]# systemctl start <service>
    

    Here's an example:

    ~]# systemctl start sshd
    
  5. Next, enable and disable the systemd services.

    To enable a systemd service, execute the following, substituting <service> with the name of the service:

    ~]# systemctl enable <service>
    

    Here's an example:

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

    To disable a systemd service, execute the following, substituting <service> with the name of the service:

    ~]# systemctl disable <service>
    

    Here's an example:

    ~]# systemctl disable sshd
    rm '/etc/systemd/system/multi-user.target.wants/sshd.service'
    ~]#
    
  6. Now, configure a service to restart when crashed.

    Let's make the ntpd service restart if it crashes after 1 minute.

    1. First, create the directory, as follows: /etc/systemd/system/ntpd.service.d.
      ~]# mkdir -p /etc/systemd/system/ntpd.service.d
      
    2. Create a new file in that directory named restart.conf and add the following to it:
      [Service]
      Restart=on-failure
      RestartSec=60s
    3. Next, reload the unit files and recreate the dependency tree using the following command:
      ~]# systemctl daemon-reload
      
    4. Finally, restart the ntpd service by executing the following command:
      ~]# systemctl restart ntpd
      

There's more…

When requesting the status of a service, the most recent log entries are also shown when executed as root.

The service status information can be seen in the following table:

Field

Description

Loaded

This provides information on whether the service is loaded and enabled. It also includes the absolute path to the service file.

Active

This provides information on whether the service is running, followed by the time it started.

Main PID

This provides PID of the corresponding service, followed by its name.

Status

This provides information about the corresponding service.

Process

This provides information about the related process.

Cgroup

This provides information about related control groups.

In some (rare) cases, you want to prevent a service from being started, either manually or by another service; there is an option to mask the service, which is as follows:

~]# systemctl mask <service>

To unmask, execute the following:

~]# systemctl unmask <service>

When modifying service unit files (and this is not limited to services only), it is best practice to copy the original service file, which is located at /lib/systemd/system to /etc/systemd/service. Alternatively, you can create a directory in /etc/systemd/service appended with .d, in which you will create conf files containing only the directives that you wish to add or change, as in the previous recipe. The advantage of the latter is that you don't need to keep up with changes in the original service file as it will be "updated" with whatever is located in the service.d directory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset