Useful Systemctl Commands to Manage Systemd Services on Linux
Systemd is rapidly replacing the convoluted, custom init systems of most Linux distributions. With Systemd installed, everything from services to filesystem mounts and network interfaces can be configured via standard unit files. Not only does Systemd change how the operating system itself starts, but it also allows for user sessions to configure their own services and cron-like timers via the exact same mechanisms. If Systemd seems confusing and overwhelming, here are a few basic commands to help you get started.
To complete this guide, you will need the following:
• 1 Node (Cloud Server or Dedicated Server) running a Systemd-compatible distribution. At time of writing, this includes Debian, CentOS, Fedora, Ubuntu, CoreOS and many others.
At its conclusion, you’ll know what you need to interact with the services installed on your Systemd distribution.
In this example, we’ll refer to Apache (httpd) as the service with which we’ll interact. It is not necessary to have Apache installed however, and it can be substituted for any service available on your Systemd-compatible distribution.
Services are most often started, stopped, reloaded or restarted. Here are the relevant systemctl commands to perform these tasks.
systemctl start httpd.service
systemctl restart httpd.service
systemctl reload httpd.service
systemctl stop httpd.service
You may also wish to check a service’s status. This prints lots of useful details about the service in addition to whether it is running. You’ll also see its relevant PIDs, exit status, why the service failed if it has, and the last few lines of the service’s log.
systemctl status httpd.service
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: active (running) since Fri 2016-06-17 09:52:07 UTC; 51s ago
Main PID: 19839 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
??19839 /usr/sbin/httpd -DFOREGROUND
??19840 /usr/sbin/httpd -DFOREGROUND
??19841 /usr/sbin/httpd -DFOREGROUND
??19842 /usr/sbin/httpd -DFOREGROUND
??19843 /usr/sbin/httpd -DFOREGROUND
??19844 /usr/sbin/httpd -DFOREGROUND
Jun 17 09:52:07 bind systemd: Starting The Apache HTTP Server...
Jun 17 09:52:07 bind systemd: Started The Apache HTTP Server.
You’ll also commonly wish to enable or disable services to start on boot. Most Systemd-compatible distributions don’t assume that any installed service should be launched automatically, and leave that choice up to the individual administrator. Here is how you’d enable the httpd service to start on boot, or how you’d undo that change later. Note that this step is distinct from starting or stopping the service right now, as we performed above.
systemctl enable httpd.servive
systemctl disable httpd.service
Maybe you wish to see a list of all active service units. A unit is Systemd’s name for a configuration file, which includes details on mountpoints, timers, network interfaces, services and more. Here is how you’d list all active service units.
Systemd can also declaratively track service dependencies. Services can depend on other services, mountpoints, or even generic states of system initialization like network availability. Here is how you’d view the dependency tree for the httpd service.
systemctl list-dependencies httpd.service
Systemd may seem complex, but it is quite simple to reason about once you’ve understood its general structure. The above should be enough to set you on the path toward understanding how to use Systemd. If this guide was helpful to you, kindly share it with others who may also be interested.