Using the systemctl Command

The most important command for managing services on a RHEL 7 (systemd) system is the systemctl command. Here are some examples of the systemctl command (using the nfs-server service as an example) and a few other commands that you may find useful:

  • Checking service status: To check the status of a service (for example, nfs-server.service), type the following:

    # systemctl status nfs-server.service
    nfs-server.service - NFS Server
       Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled)
       Active: active (exited) since Wed 2014-03-19 10:29:40 MDT; 57s ago
      Process: 5206 ExecStartPost=/usr/libexec/nfs-utils/scripts/nfs-server.postconfig (code=exited, status=0/SUCCESS)
      Process: 5191 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS $RPCNFSDCOUNT (code=exited, status=0/SUCCESS)
      Process: 5188 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
      Process: 5187 ExecStartPre=/usr/libexec/nfs-utils/scripts/nfs-server.preconfig (code=exited, status=0/SUCCESS)
     Main PID: 5191 (code=exited, status=0/SUCCESS)
       CGroup: /system.slice/nfs-server.service
    Mar 19 10:29:40 localhost.localdomain systemd[1]: Starting NFS Server...
    Mar 19 10:29:40 localhost.localdomain systemd[1]: Started NFS Server.
  • Stopping a service: To stop a service, use the stop option as follows:

    # systemctl stop nfs-server.service
  • Starting a service: To start a service, use the start option as follows:

    # systemctl start nfs-server.service
  • Enabling a service: To enable a service so it starts automatically at boot time, type the following:

    # systemctl enable nfs-server.service
  • Disable a service: To disable a service so it doesn't start automatically at boot time, type the following:

    # systemctl disable nfs-server.service
  • Listing dependencies: To see dependencies of a service, use the list-dependencies option, as follows:

    # systemctl list-dependencies nfs-server.service
  • listing units: To see all known units(see below section for more options)# systemctl list-units
    UNIT                                          LOAD   ACTIVE SUB       DESCRIPTION
    proc-sys-fs-binfmt_misc.automount             loaded active waiting   Arbitrary Executable File Formats File System
    sys-devices-pci0000:00-0000:00:08.0-net-net0.device loaded active plugged   MCP55 Ethernet
    sys-devices-pci0000:00-0000:00:09.0-net-net1.device loaded active plugged   MCP55 Ethernet
  • Listing units in targets: To see what services and other units (service, mount, path, socket, and so on) are associated with a particular target, type the following:

    # systemctl list-dependencies
  • List specific types of units: Use the following command to list specific types of units (in these examples, service and mount unit types):

    # 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 
    accounts-daemon.service      loaded active running Accounts Service
    # systemctl list-units --type mount 
    UNIT                         LOAD   ACTIVE SUB     DESCRIPTION
    -.mount                      loaded active mounted /
    boot.mount                   loaded active mounted /boot
    dev-hugepages.mount          loaded active mounted Huge Pages File System
    dev-mqueue.mount             loaded active mounted POSIX Message Queue File Syst
    mnt-repo.mount               loaded active mounted /mnt/repo
    proc-fs-nfsd.mount           loaded active mounted RPC Pipe File System
    run-user-1000-gvfs.mount     loaded active mounted /run/user/1000/gvfs
  • Listing all units: To list all units installed on the system, along with their current states, type the following:

    # systemctl list-unit-files
    UNIT FILE                            STATE
    proc-sys-fs-binfmt_misc.automount    static
    dev-hugepages.mount                  static
    dev-mqueue.mount                     static
    proc-sys-fs-binfmt_misc.mount        static
    arp-ethers.service                   disabled
    atd.service                          enabled
    auditd.service                       enabled
  • View service processes with systemd-cgtop: To view processes associated with a particular service (cgroup), you can use the systemd-cgtop command. Like the top command (which sorts processes by such things as CPU and memory usage), systemd-cgtop lists running processes based on their service (cgroup label). Once systemd-cgtop is running, you can press keys to sort by memory (m), CPU (c), task (t), path (p), or I/O load (i). Here is an example:

    # systemd-cgtop
  • Recursively view cgroup contents: To output a recursive list of cgroup content, use the systemd-cgls command:

    # systemd-cgls
    │ ├─user-1000.slice
    │ │ ├─session-5.scope
    │ │ │ ├─2661 gdm-session-worker [pam/gdm-password]
    │ │ │ ├─2672 /usr/bin/gnome-keyring-daemon --daemonize --login
    │ │ │ ├─2674 gnome-session --session gnome-classic
    │ │ │ ├─2682 dbus-launch --sh-syntax --exit-with-session
    │ │ │ ├─2683 /bin/dbus-daemon --fork --print-pid 4 --print-address 6 --session
    │ │ │ ├─2748 /usr/libexec/gvfsd
  • View journal (log) files: Using the journalctl command you can view messages from the systemd journal. Using different options you can select which group of messages to display. The journalctl command also supports tab completion to fill in fields for which to search. Here are some examples:

    # journalctl -h     View help for the command
    # journalctl -k     View kernel messages from current boot
    # journalctl -f     Follow journal messages (like tail -f)
    # journalctl -u NetworkManager     View messages for specific unit (can tab complete)