Linux Commands

Restart a Service using systemctl restart Command

The systemd is a system service manager that is widely used on Linux. To manage systemd services, the systemctl command line utility is used. This tool is also used to restart any systemd service.

Services on Linux are managed through their configuration file. If modifications are made to a service’s configuration file, it is necessary to initiate a restart of the service to apply the modifications. Upon restarting the service, the systemd re-evaluates the configuration file and applies the modifications.

In this guide, I will be going through how to use the systemctl to restart a service in Linux.

Note: This guide includes commands and instructions that are executed on Ubuntu 22.04. The command will work without any issues on distributions that come with the systemd service manager.

The systemctl restart Command

The restart command essentially stops a service and starts it again. If the service or unit is not operational, the restart command will initiate its operation.

The restart command does not remove the processes that are linked to the service. Take the example of file descriptors, which are non-negative identifiers assigned by the operating system to the files opened by a service. If you restart a service, the file descriptor linked to that service will remain there during the restart process.

If you want to flush out all the linked processes to the service, then you need to explicitly stop the service and start it again.

How to Restart a Service

On Linux, you can restart a service by using the sudo systemctl command line tool with the restart option and specifying its name. The general syntax is given below:

sudo systemctl restart [service-name]

You will need sudo privileges to restart a service.

For example, let’s restart the ssh service.

sudo systemctl restart ssh.service

To restart multiple services, append each service name after the restart command with a space.

sudo systemctl restart ssh.service smbd.service

Other restart commands are given in the table below:

try-restart It stops or starts the specified service or services and if the service is not running it does not start it
reload-or-restart Reload the supported service or services and if the service is not supported then restart it and activate it
try-reload-or-try-restart Reload the supported service or services and if the service is not supported then restart it without activating it

In the above commands, the .service extension is optional.

How to Automatically Restart a Service

On Linux, if a service fails, then the systemd restarted it by default. Nonetheless, in many cases, it may be necessary to modify the manner in which a service restarts. For instance, one may need manual action during the debugging process of the custom-made service.

The systemd unit files or service files are located on /etc/systemd/system or /lib/systemd/system mainly depending on how the service is created. To list the unit files on Linux, use the following command:

ls /lib/systemd/system

You can see the configuration files of different services. Let’s open the ssh.service file using the nano editor.

sudo nano /lib/systemd/system/ssh.service

Here you can modify the Restart setting. By default, it is set to on-failure. Other options to restart a service are listed below:

  • no
  • always
  • on-success
  • on-failure
  • on-abnormal
  • on-abort
  • on-watchdog

Another option in the unit service file is RestartSec which is used to specify the number of seconds after which the service will restart.

[Service]

Restart=always

RestartSec=5

These instructions mean that the service will restart on boot and, if interrupted, will be restarted after 5 seconds.

After modifying the setting, execute the following command to apply the changes.

sudo systemctl reload-daemon

Other important settings to take into consideration are StartLimitIntervalSec and StartLimitBurst. These options are useful to set the maximum time and maximum retries to restart a service.

[Unit]

StartLimitIntervalSec=300

StartLimitBurst=4

The above instruction indicates that the systemd will automatically stop trying to restart a service if it does not start after 300 seconds and 4 retries.

To verify if the service restarts after 5 seconds or not, kill the service using the PID of the service and the kill command.

sudo kill -9 [PID]

After 5 seconds, the service will be restarted; use the journalctl command to check the status of the ssh.service.

journalctl -u ssh.service

How to Restart a Service when Dependent Service Restarts

On Linux, many services are interdependent, and similarly, they are required to be restarted when a dependent service is restarted.

There are three different options in the unit service file to restart a service, with a dependent service restarted.

  • PartOf
  • BindsTo
  • Requires

All of these options perform the same task.

Let’s take an example of ssh.service which depends upon the apparmor.service; a Linux Security Module to provide necessary access. To list the dependencies of a service in Linux use systemctl with list-dependencies command and service name.

systemctl list-dependencies ssh.service

So, if you want to restart the ssh.service when you restart the apparmor.service, then you need to include the PartOf, BindsTo, or Requires option along with the service name in the [Unit] section of the apparmor.service file.

Open the apparmor.service file.

sudo nano /lib/systemd/system/apparmor.service

Add the following line in the [Unit] section.

PartOf=ssh.service

Save the file and execute the daemon-reload command.

sudo systemctl daemon-reload

Now, restart the ssh.service and then check the apparmor.service log.

You will notice the apparmor.service restarted at the same time the ssh.service restarted.

Conclusion

To restart a service or services on Linux, the systemctl command is used with the restart option. The systemctl is a command line utility used to manage systemd services. The restart option starts a service and then stops it, activating an inactive service. However, you can restart a service without activating it by using the try-restart option.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.