systemctl Commands
From: https://www.linode.com/docs/guides/introduction-to-systemctl/





systemctl Commands: Restart, Reload,
and Stop Service
Published August 31, 2018 by Linode
Create a Linode account to try this guide with a $100 credit.
This credit will be applied to any valid services used during your first 60
days.



What is systemctl? systemctl is a controlling interface and inspection tool for the widely -adopted init system and service manager systemd. This guide will cover how to use systemctl to manage systemd services, work with systemd Targets and extract meaningful information about your system’s overall state. Note This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, see the Users and Groups guide.
Managing Services systemd initializes user space components that run after the Linux kernel has booted, as well as continuously maintaining those components throughout a system’s lifecycle. These tasks are known as units, and each unit has a corresponding unit file. Units might concern mounting storage devices (.mount), configuring hardware (.device), sockets (.socket), or, as will be covered in this guide, managing services (.service).
Starting and Stopping a Service To start a systemd service in the current session, issue the start command: sudo systemctl start apache2.service Conversely, to stop a systemd service, issue the stop command: sudo systemctl stop apache2.service In the above example we started and then stopped the Apache service. It is important to note that systemctl does not require the .service extension when working with service units. The following is just as acceptable: sudo systemctl start apache2 If the service needs to be restarted, such as to reload a configuration file, you can issue the restart command: sudo systemctl restart apache2 Similarly, if a service does not need to restart to reload it’s configuration, you can issue the reload command: sudo systemctl reload apache2 Finally, you can use the reload-or-restart command if you are unsure about whether your application needs to be restarted or just reloaded. sudo systemctl reload-or-restart apache2
Enabling a Service at Boot The above commands are good for managing a service in a single session, but many services are also required to start at boot. To enable a service at boot: sudo systemctl enable nginx To disable the service from starting at boot, issue the disable command: sudo systemctl disable nginx Note The enable command does not start the service in the current session, nor does disable stop the service in the current session. To enable/disable and start/stop a service simultaneously, combine the command with the --now switch: sudo systemctl enable nginx --now If the service unit file is not located within one of the known systemd file paths, you can provide a file path to the service unit file you wish to enable: sudo systemctl enable /path/to/myservice.service However, this file needs to be accessible by systemd at startup. For example, this means files underneath /home or /var are not allowed, unless those directories are located on the root file system.
Checking a Service’s Status systemctl allows us to check on the status of individual services: systemctl status mysql This will result in a message similar to the output below: mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2018-08-30 09:15:35 EDT; 1 day 5h ago Main PID: 711 (mysqld) Tasks: 31 (limit: 2319) CGroup: /system.slice/mysql.service └─711 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid You can also use is-active, is-enabled, and is-failed to monitor a service’s status: systemctl is-enabled mysql To view which systemd service units are currently active on your system, issue the following list-units command and filter by the service type: systemctl list-units --type=service Note list-units is the default action for the systemctl command, so you can simply enter systemctl to retrieve a list of units. The generated list includes all currently active service units, service units that have jobs pending, and service units that were active and have failed:
UNITLOADACTIVESUBDESCRIPTION
accounts-daemon.serviceloadedactiverunningAccounts Service
apparmor.serviceloadedactiveexitedAppArmor initialization
apport.serviceloadedactiveexitedLSB: automatic crash report generation
atd.serviceloadedactiverunningDeferred execution scheduler
blk-availability.serviceloadedactiveexitedAvailability of block devices
console-setup.serviceloadedactiveexitedSet console font and keymap
cron.serviceloadedactiverunningRegular background program processing daemon
dbus.serviceloadedactiverunningD-Bus System Message Bus
ebtables.serviceloadedactiveexitedebtables ruleset management
...
The output provides five pieces of data: To list all units, including inactive units, append the --all flag: systemctl list-units --type=service --all You can filter the list of units by state. Supply a comma-separated list of unit states to output as the value for the --state flag: systemctl list-units --type=service --all --state=exited,inactive To retrieve a list of failed units, enter the list-units command with the - -failed flag: systemctl list-units --failed
Working with Unit Files Each unit has a corresponding unit file. These unit files are usually located in the following directories:
Listing Installed Unit Files Not all unit files are active on a system at any given time. To view all systemd service unit files installed on a system, use the list-unit-files command with the optional --type flag: systemctl list-unit-files --type=service The generated list has two columns, UNIT FILE and STATE:
UNIT FILESTATE
accounts-daemon.serviceenabled
acpid.servicedisabled
apparmor.serviceenabled
apport-forward@.servicestatic
apt-daily-upgrade.servicestatic
apt-daily.servicestatic
...
A unit’s STATE can be either enabled, disabled, static, masked, or generated. Unit files with a static state do not contain an Install section and are either meant to be run once or they are a dependency of another unit file and should not be run alone. For more on masking, see Masking a Unit File.
Viewing a Unit File To view the contents of a unit file, run the cat command: systemctl cat cron # /lib/systemd/system/cron.service [Unit] Description=Regular background program processing daemon Documentation=man:cron(8) [Service] EnvironmentFile=-/etc/default/cron ExecStart=/usr/sbin/cron -f $EXTRA_OPTS IgnoreSIGPIPE=false KillMode=process [Install] WantedBy=multi-user.target If there are recent changes to the unit file that have not yet been loaded into systemd, the output of the systemctl cat command may be an older version of the service. For a low-level view of a unit file, issue the show command: systemctl show cron This will generate a list of property key=value pairs for all non-empty properties defined in the unit file: Type=simple Restart=no NotifyAccess=none RestartUSec=100ms TimeoutStartUSec=1min 30s TimeoutStopUSec=1min 30s RuntimeMaxUSec=infinity ... To show empty property values, supply the --all flag. To filter the key=value pairs by property, use the -p flag: systemctl show cron -p Names Note that the property name must be capitalized.
Viewing a Unit File’s Dependencies To display a list of a unit file’s dependencies, use the list-dependencies command: systemctl list-dependencies cron The generated output will show a tree of unit dependencies that must run before the service in question runs. cron.service ● ├─system.slice ● └─sysinit.target ● ├─apparmor.service ● ├─blk-availability.service ● ├─dev-hugepages.mount ● ├─dev-mqueue.mount ● ├─friendly-recovery.service ... Recursive dependencies are only listed for .target files. To list all recursive dependencies, pass in the --all flag. To check which unit files depend on a service unit file, you can run the list-dependencies command with the --reverse flag: systemctl list-dependencies cron --reverse
Editing a Unit File Note While the particulars of unit file contents are beyond the scope of this article, there are a number of good resources online that describe them, such as the RedHat Customer Portal page on Creating and Modifying systemd Unit Files. There are two ways to edit a unit file using systemctl.