How to create and manage services in CentOS 7 with systemd

Last updated on 22nd February 2018

Systemd is a system and service manager and like most major Linux distributions the init deamon was replaced by systemd in CentOS 7. One of the main functions of systemd is to manage the services, devices, mount points, sockets and other entities in a Linux system. Each of these entity that are managed by systemd is called a unit. Each unit is defined by a unit file (configuration file) which is located in one of the following directories.

DirectoryDescription
/usr/lib/systemd/system/Unit files distributed with installed packages. Do not modify unit files in this location.
/run/systemd/system/Unit files that are dynamically created at runtime. Changes in this directory are lost when rebooted.
/etc/systemd/system/Unit files created by systemctl enable and custome unit files created by system administrators.

Any custom unit files that you create should be placed in the /etc/system/system/ directory. This directory takes precedence over other directories.

Unit files names are of the form

unit_name.unit_type

Unit_type can be one of the following:

Unit TypeDescription
deviceA device unit.
serviceA system service.
socketA socket for inter-process communication.
swapA swap file or device.
targetA group of units.
timerA systemd timer.
snapshotA snapshot of systemd manager.
mountA mount point.
sliceA group of unit that manage the system processes.
pathA file or directory.
automountA automount point.
scopeAn externally created process.

Creating a new service (systemd unit)

To create a custom service to be managed by systemd, you create a unit file that defines the configuration of that service. To create a service named MyService for example, you create a file named MyService.service in /etc/systemd/system/

# vi /etc/systemd/system/MyService.service

The unit file of service consists of a set of directives that are organized in to three sections - Unit, Service and Install. Below is an example of a very simple unit file.

[Unit]
 Description=Service description

[Service]	
 ExecStart=path_to_executable

[Install]
 WantedBy=default.target

Once you have created the unit file with all the necessary configuration options, save the file and set the correct file permissions.

# chmod 664 /etc/systemd/system/MyService.service

The next step is to reload all unit files to make systemd know about the new service.

# systemctl daemon-reload

Finally to start the service, run

# systemctl start MyService.service

[Unit] Section

The following are the main directives that you specify in the [Unit] section.

DescriptionA short description of the unit.
DocumentationA list of URIs pointing to the documentation for the unit.
RequiresA list of units that must be started alongside the current unit. If the any these units fail to start then current unit will not be activated.
WantsSimilar to the Requires directive but the difference is the current unit will be activated even if the depended units fail to start.
BeforeList of units that cannot be started before the current unit.
AfterThe current unit can started only after the units listed here.
ConflictsList units that cannot be run concurrently with the current unit.

[Service] Section

Some of the common directives that you'll see in service section are.

TypeDefines the startup type of the unit which can be one of the values:
  • Simple: This is the default. The main process of the service is the process started with ExecStart.
  • Forking: The process started with ExecStart spawns a new child process which becomes the main process and the parent process is terminated when the startup is completed.
  • Onehot: Similar to simple but systemd waits for the process to exit before proceeding with other units.
  • Dbus: Similar to simple but systemd waits for the process to take a name on the dbus.
  • Notify: Similar to simple Systemd will wait for a notification from the process before continuing with other units.
  • Idle: Similar to simple but service will not run until all other jobs are finished.
ExecStartSpecifies the command to the executed to start service.
ExecStartPreSpecifies the command to be executed before the main process specified in the ExecStart is started.
ExecStartPostSpecifies the command to be executed after the main process specified in the ExecStart has finished.
ExecStopSpecifies the command to be executed when the service is stopped.
ExecReloadSpecifies the command to be executed when the service is restarted.
RestartSpecifies when to restart the service automatically. Possible values are "always", "on-success", "on-failure", "on-abnormal", "on-abort", or "on-watchdog".

[Install] Section

The [install] section provides information required to enable or disable the units using the systemctl command. The common options are:

RequiredByA list of units that requires unit. A symbolic link of this unit is created in the .requires directory of the listed unit.
WantedBySpecifies a list of targets under which the service should be started. A symbolic link of this unit is created in the .wants directory of the listed target.

Using systemctl to manage services

systemctl is the command line tool you can use to control and manage services in systemd. Let's now take a look at the some of the important systemctl commands for service management.

Listing Service Units and Unit files

To list all the units that are loaded

# systemctl list-units

To list only units of type service

# systemctl list-units -t service

To list all installed unit files of type service

# systemctl list-unit-files -t service

To list all installed unit files of type service

# systemctl list-unit-files -t service

You can use the --state option to filter the output by the state of the unit. The following command lists all services that are enabled.

# systemctl list-unit-files --state enabled

Note the difference between list-units and list-unit-files is that list-unit will only show units that are loaded while list-unit-files shows all unit files that are installed on the system.

Start and Stop service

This is quite straightforward, start option to start a service and stop option to stop a service

# systemctl start service_name.service
# systemctl stop service_name.service

Restart and Reload services

The restart option will restart a service that is running. If the service is not running, it will be started.

# systemctl restart service_name.service

If you want to restart the service only if its running then use the try-restart option.

# systemctl try-restart service_name.service

The reload option will try to reload the service specific configuration of a unit if it is supported.

# systemctl reload service_name.service

Enable and Disable services

Units can be enabled or disabled using the enable or disable options of systemctl command. When a unit a enabled symbolic links are created in various locations as specified in the [install] section of the unit file. Disabling a unit will remove the symbolic links that wer created when the unit was enabled.

# systemctl enable service_name.service
# systemctl disable service_name.service

Reload Unit Files

Whenever you make any changes to the unit files you need to let systemd know by executing daemon-reload which reloads all unit files.

# systemctl daemon-reload

Modifying system services

The unit files that come with installed packages are stored in /usr/lib/systemd/system/. The unit files in this directory should not be modified directly as the changes will be lost when if you update the package. The recommended method is to first copy the unit file to /etc/systemd/system/ and make the changes in that location. The unit files in /etc/systemd/system/ takes precedence over unit files in /usr/lib/systemd/system/ so the original unit file will be overridden.


Post a comment

Comments

Asif Ayub Mayo | August 31, 2018 8:57 AM |

Thanks!, You explain it better than documentations.Good aritcle.