How to Start, Stop, and Restart Linux Services With systemctl

restart services in linux

In Linux environments, almost everything is a service. In this blog, we will learn how to start, stop & restart services in Linux.

For instance, system login is managed by local or network services depending on whether you are logging in as a local or remote user. Similarly, process scheduling is handled by cron, another system service. 

Likewise, almost all applications you use on a Linux system are a combination of services that interact with other services and devices. 

That’s why Linux offers detailed management of system services with systemd, a comprehensive suite of system components that unify system service management operations. Users can access these via the systemctl command. All users and processes can use this interface to stop and restart a service in the Linux system. 

In this article, we will show you how to use systemctl and related commands to control service behavior. We will see how to activate, deactivate, start, and restart a service in Linux distributions, such as Debian 7, CentOS 7, or Ubuntu 16.04 and newer versions.

Table Of Contents

  1. A Short Overview of the systemctl Command
  2. How to Use systemctl to Start, Stop, and Restart Services in Linux
    1. The Prerequisites
    2. Use Case #1: Verify That a Service is Running
    3. Use Case #2: Restart a Service
    4. Use Case #3: Start a Service
    5. Use Case #4: Stop a Service
    6. Use Case #5: Enable Service at Boot
    7. Use Case #6: Disabling Service at Boot
  3. Understanding Variations in Service Names
  4. Conclusion
  5. FAQs

A Short Overview of the systemctl Command

Managing services is a critical aspect of working with Linux systems. systemctl offers a simple way of controlling application behavior and restarting a service with a single command. 

The basic syntax of the systemctl command is as follows:

# systemctl [command] [service_name]

Important: You need at least sudo privileges to run these commands because of the direct impact on system security and functionality.

How to Use systemctl to Start, Stop, and Restart Services in Linux

Let’s now explore the applications of the systemctl command in managing services. 

The Prerequisites

Before starting, you’ll need:

  • A computer with a mainstream Linux distribution
  • A user account with sudo or root permissions

Use Case #1: Verify That a Service is Running

The most common usage of the command is to see if a service is running. The syntax of this command is as follows:

# sudo systemctl status apache2

sudo systemctl status apache2

You can see the status of the service in the output. 

Use Case #2: Restart a Service

You often need to restart a service after you make changes to the configuration or similar areas. We recommend the following command syntax to restart a service in Linux:

#sudo systemctl restart SERVICE_NAME

After restarting a service, it is a good idea to make sure the service is running by using the status command.

For instance, here is the command we used to restart the Apache service on our test system: 

# sudo systemctl restart apache2

# sudo systemctl status apache2

sudo systemctl restart apache2 sudo systemctl status apache2

Alternatively, you can use the reload command to reload a service: 

# sudo systemctl reload apache2

# sudo systemctl status apache2

sudo systemctl reload apache2 sudo systemctl status apache2

Use Case #3: Start a Service

While many services in a Linux system are on self-start, you may need to manually initiate a service. For this, use the following command in the terminal:

#sudo systemctl start SERVICE_NAME

For instance, run the following command to start the Apache service:

#sudo systemctl start apache2

Use Case #4: Stop a Service

We recommend stopping a service before you make changes to an application’s configurations. You can use the following command syntax to stop an active service in Linux:

#sudo systemctl stop SERVICE_NAME

Continuing the example in the previous section, run the following command to stop the Apache service:

#sudo systemctl stop apache2

After halting a service, it is a good idea to check the status of the service to verify if it has stopped running. 

For this use the following command:

# sudo systemctl status apache2

The output should indicate that the service is inactive (dead).

sudo systemctl stop apache2

Use Case #5: Enable Service at Boot

You can configure a service to start automatically when the system boots. We recommend this command syntax:

#sudo systemctl enable SERVICE_NAME

Continuing th example of the previous sections, you can use the following command to enable Apache service at the system boot:

#sudo systemctl enable apache2

sudo systemctl enable apache2

Use Case #6: Disabling Service at Boot

You can prevent a service from starting at boot with the following command syntax:

#sudo systemctl disable SERVICE_NAME

Consider the following example that disables the Apache service at system boot:

#sudo systemctl disable apache2

Understanding Variations in Service Names

As you become more familiar with a specific Linux environment, you’ll become accustomed to the service names you frequently use.

For instance, if you’re developing a website, you’ll likely use systemctl restart apache2 regularly as you update server configurations.

However, it’s essential to recognize that service names can differ when transitioning between different Linux distributions.

For instance, in Ubuntu and other Debian-based distributions, the Apache service is identified as apache2. Conversely, in CentOS 7 and other RedHat-based distributions, it’s referred to as httpd or httpd.service.

Understanding Variations in Service Names

Conclusion

Mastering service management in Linux is crucial for smooth system operation. By using commands like systemctl start, stop, and restart, users can effectively control system components and troubleshoot issues.

Understanding service name variations across Linux distributions is vital for seamless navigation and resource utilization.

With practice, users can confidently manage services, ensuring system stability and productivity in Linux environments.

For expert Linux support, consider RedSwitches. 

We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

Contact us today for professional assistance with server management, security, and optimization.

FAQs

Q. What does the systemctl command do in Linux?

The systemctl command is a pivotal tool in Linux for managing the systemd system and service manager. It allows you to control the state of systemd services and the system itself, providing commands to start, stop, restart, and check the status of services.

Q. How can you restart a service using systemctl?

Restarting a service with systemctl is straightforward. Execute sudo systemctl restart service_name in a terminal window, replacing service_name with the actual name of the service you wish to restart.

Q. What are the steps to stop and then restart a service in Linux using systemctl?

To stop a service, use sudo systemctl stop service_name. To restart it, use sudo systemctl restart service_name, substituting service_name with the name of the service you’re managing.

Q. How can you verify if a service is active in Linux?

To check a service’s activity status, use systemctl is-active service_name in the terminal, where service_name is the name of the service you’re querying about.

Q. How do you enable a service to start on boot using systemctl?

To have a service start automatically at boot, use sudo systemctl enable service_name, with service_name being the name of the service you want to enable.

Q. What distinguishes sysvinit from systemd in Linux?

sysvinit, the older initialization system for Linux, operates with a sequential startup process. systemd, the newer init system, offers parallel startup tasks, dependency tracking, and more dynamic control, making it the standard in most current Linux distributions.

Q. In what ways can you manage services using the systemctl command?

The systemctl command allows for comprehensive service management, including starting, stopping, restarting, enabling, disabling, and checking the status of systemd services.

Q. How can the concept of ‘flow of air’ relate to systemd services management?

Though not a direct term in systemd management, ‘flow of air’ metaphorically could represent the smooth and unobstructed operation of services, ensuring that they run without interruption, similar to an uninterrupted flow of air.

Q. Can you provide examples or resources for learning systemctl commands?

Many online community platforms like Stack Exchange offer examples, discussions, and audio links to help understand the usage of systemctl commands. These resources often include practical examples in English terms, aiding in comprehension.

Q. Is it possible to receive daily email notifications about the status of a systemd service?

Yes, you can configure your Linux system to send daily emails with the status of a systemd service by setting up a cron job that runs systemctl status service_name and mails the output to a specified email address.