Useful Systemctl Commands to Manage Systemd Services on Linux

Try it in our public cloud & Get $50 Credit
CLAIM NOW

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.

Getting 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.

Tutorial

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
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 19839 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
??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[1]: Starting The Apache HTTP Server...
Jun 17 09:52:07 bind systemd[1]: 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.

systemctl

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
httpd.service
??system.slice
??basic.target
??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
? ??dbus.socket
? ??rpcbind.socket
? ??systemd-initctl.socket
? ??systemd-journald.socket
? ??systemd-shutdownd.socket
? ??systemd-udevd-control.socket
? ??systemd-udevd-kernel.socket
[...]

Conclusion

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.