How to start, stop and restart services in Linux

by admin
How to start, stop and restart services in Linux

For advanced Linux users, starting, stopping and restarting Linux services is essential. These operations allow users to access the functionalities of each service. For example, to use a web server, users must start the Apache service, or to use a database, users must start the MySQL service. Managing Linux services is also important for system stability and can help improve system performance.

Despite common opinion, starting, stopping, and restarting services in Linux is relatively simple. We will work with Linux, but all commands to start, stop and restart Linux services can be run on CentOS, Ubuntu, Redhat, Fedora, Debian and many other distributions.

What is the difference between systemctl and service commands?

There are two official management tools that provide a consistent way to start, stop, restart, and manage system services in Linux:

Systemctl offers more advanced features, including dependency management, enabling/disabling services, and integration with journalctl for logging. The service is simpler and primarily used for basic service start, stop, and status commands. It is often used with older SysVinit-based systems.

Which one you use will depend on whether your distribution uses systemd or init. Most modern distributions now use systemd, so systemctl is the service manager of choice. But some old habits die hard, which is why many administrators still cling to the aging service order.

Fortunately, the systemd developers made sure to keep the service and redirect it to systemctl, so even on systemd-based systems, using service will still work for basic tasks.

To complicate matters further, you might find a random service you installed that hasn’t been updated with service tools or systemctl and have to start it manually with /etc/rc.d (or /etc/init .d). But we’re looking for best practices here, and for starting, stopping, or restarting applications in Linux, best practices start and end with systemctl.

SEE: Start learning how to use Linux for IT and system administrator with this pack

Starting a Linux service

Let’s say you want to start the Apache server.

To do this:

  1. Open a terminal window.
  2. Run the command sudo systemctl start httpd.

In this command:

  • sudo tells Linux that you are running the command as root.
  • systemctl manages systemd services.
  • start tells the systemctl command to start the Apache service.
  • httpd is the service name of the Apache web server.
  1. Once the command is executed, you will get the following message:

The service httpd has started successfully.

Note If the service is already running, you will see the following message:

The service httpd is already running.

SEE: How to Quickly Open a Terminal in a Specific Linux Directory

Common error messages

Failed to start httpd.service. Unit httpd.service not found.

This error occurs if the Apache web server package is not installed or the service unit file is missing. Install the Apache package using sudo apt install apache2 (on Debian-based systems) or sudo yum install httpd (on Red Hat-based systems) to resolve it.

Failed to start httpd.service. Address already in use.

This indicates that another process is already using the port Apache wants to bind to (usually port 80). Identify the conflict process with sudo lsof -i:80 and stop it, or change the port configuration in the Apache configuration file.

Stop a Linux service

To stop the Apache service:

  1. Open a terminal window
  2. Run the command sudo systemctl stop httpd.
  3. You should now see the following message:

The service httpd has been stopped successfully.

Note that if the service, in this case Apache, was not running, you will get the following message:

Failed to stop service httpd. Unit httpd.service is not loaded.

Install it using sudo apt install apache2 (based on Debian) or sudo yum install httpd (Based on Red Hat).

Or you may receive one of the following messages:

Failed to stop service httpd. Unit httpd.service is not running.

This indicates that Apache is already stopped and no action is required.

Failed to stop service httpd. Unit httpd.service is in a failed state.

This suggests that Apache has encountered an error and is failing. To troubleshoot, run sudo journalctl -xe to view the detailed logs, then try restarting the service.

Failed to stop service httpd. Unit httpd.service is locked.

This error occurs if another process is controlling the service. Wait briefly and try again, or check the running management tasks with ps aux | grep httpd to identify the locking process.

SEE: Linux 101: How to Find Files from the Linux Command Line

Restart a Linux service

To restart the same service (Apache):

  1. Open a terminal window.
  2. Run the command sudo systemctl restart httpd.
  3. The service will restart and you will be returned to the bash prompt.
  4. You will get the following message:

The service httpd has been restarted successfully.

Common error messages

If the Apache service is not running, you will see the following result:

The service httpd is not running.

You can start it directly with sudo systemctl start httpd or check its status with systemctl status httpd.

You may also see the following:

Job for httpd.service failed.

This usually indicates a configuration or dependency issue. To resolve the issue, review the error details with sudo journalctl -xe and correct any configuration issues.

Starting, stopping, and restarting services with service usage

To make things interesting, the service command still works, even for distributions that have migrated to systemd and systemctl. This means that those who instinctively type service when they need to restart a service on Linux will not receive a message. Unknown command error.

SEE: Run a Google Search from the Linux Command Line with Googler

In case of service, the command will redirect to systemctl. In fact, when you run the service command on a systemctl compatible distribution, you will clearly see the redirection information.

The terminal shows you redirection information for service commands on systemctl-compatible distributions.

Using the service command is a little different from using systemctl. The service name and start, stop, and restart options are switched:

sudo service httpd start
sudo service httpd stop
sudo service httpd restart

In each case, you will see the service redirected to systemctlbut the service you are trying to start, stop, or restart will succeed.

To learn more about what systemctl can do for you, be sure to issue the man systemctl command and read the man page.

Source Link

You may also like

Leave a Comment