Managing Services with systemd

Systemd is a system and service manager for Linux operating systems. Systemd introduces the concept of systemd units. These units are represented by unit configuration files and encapsulate information about system services, listening sockets, and other objects that are relevant to the init system.

To view, start, stop, restart, enable, or disable system services, use the systemctl command.

systemctlDescription
systemctl start name.serviceStarts a service
systemctl stop name.serviceStops a service
systemctl restart name.serviceRestarts a service
systemctl try-restart name.serviceRestarts a service only if it is running
systemctl reload name.serviceReloads configuration
systemctl status name.serviceChecks if a service is running
systemctl is-active name.service
systemctl list-units —type service —allDisplays the status of all services

To only verify that a particular service unit is running, run the following command: systemctl is-active name.service

Similarly, to determine whether a particular service unit is enabled, type: systemctl is-enabled name.service

To configure a service unit that corresponds to a system service to be automatically started at boot time, type the following at a shell prompt as root: systemctl enable name.service

To prevent a service unit that corresponds to a system service from being automatically started at boot time, type the following at a shell prompt as root: systemctl disable name.service

Creating Custom Unit Files

Create a unit file in the /etc/systemd/system/ directory and make sure it has correct file permissions. Execute as root:

touch /etc/systemd/system/name.service
chmod 664 /etc/systemd/system/name.service

Open the name.service file created in the previous step, and add the service configuration options.

[Unit]
Description=service_description
After=network.target

[Service]
ExecStart=path_to_executable
Type=forking
PIDFile=path_to_pidfile

[Install]
WantedBy=default.target

Notify systemd that a new name.service file exists by executing the following command as root:

systemctl daemon-reload
systemctl start name.service

Man pages

systemctl(1) — The manual page for the systemctl command line utility provides a complete list of supported options and commands.

systemd(1) — The manual page for the systemd system and service manager provides more information about its concepts and documents available command line options and environment variables, supported configuration files and directories, recognized signals, and available kernel options.

systemd.unit(5) — The manual page named systemd.unit provides detailed information about systemd unit files and documents all available configuration options.

Source docs.redhat.com