Linux provides fine-grained control over system services through systemd, using the systemctl command. Services can be turned on, turned off, restarted, reloaded, or even enabled or disabled at boot. If you are running Debian 7, CentOS 7, or Ubuntu 15.04 (or later), your system likely uses systemd.
This guide will show you how to use basic commands to start, stop, and restart services in Linux.
Prerequisites
Access to a user account with sudo or root privileges
Access to a terminal/command line
The systemctl tool, included in Linux
Basic Syntax of systemctl Command
The basic syntax for using the systemctl command is:systemctl [command] [service_name]
Typically, you’ll need to run this as a superuser with each command starting with sudo.
How To Check If a Service is Running on Linux
To verify whether a service is active or not, run this command:sudo systemctl status apache2
Replace apache2 with the desired service. In our case, we checked the status of Apache. The output shows that the service is active (running), as in the image below:
How to Restart a Service
To stop and restart the service in Linux, use the command:sudo systemctl restart SERVICE_NAME
After this point, your service should be up and running again. You can verify the state with the status command.
To restart Apache server use:sudo systemctl restart apache2
How to Reload a Service
To force the service to reload its configuration files, type in the following command in the terminal:sudo systemctl reload SERVICE_NAME
After reloading, the service is going to be up and running. Check its state with the status command to confirm.
In our example, we reloaded Apache using:sudo systemctl reload apache2
How to Start a Service
To start a service in Linux manually, type in the following in the terminal:sudo systemctl start SERVICE_NAME
For instance, the command to start the Apache service is:sudo systemctl start apache2
How to Stop a Service
To stop an active service in Linux, use the following command:sudo systemctl stop SERVICE_NAME
If the service you want to stop is Apache, the command is:sudo systemctl stop apache2
Check whether the service stopped running with the status command. The output should show the service is inactive (dead).
How to Enable the Service at Boot
To configure a service to start when the system boots, use the command:sudo systemctl enable SERVICE_NAME
To enable Apache upon booting the system, run the command:sudo systemctl enable apache2
How to Disable Service at Boot
You can prevent the service from starting at boot with the command:sudo systemctl disable SERVICE_NAME
For example:sudo systemctl disable apache2
Variations in Service Names
If you work within the same Linux environment, you will learn the names of the services you commonly use.
For example, if you are building a website, you will most likely use systemctl restart apache2 frequently, as you refresh configuration changes to your server.
However, when you move between different Linux variants, it is helpful to know that the same service may have different names in different distributions.
For example, in Ubuntu and other Debian based distributions, the Apache service is named apache2. In CentOS 7 and other RedHat distros, the Apache service is called httpd or httpd.service.
Conclusion
In most modern Linux operating systems, managing a service is quite simple using the commands presented here.
Make sure to know the name of the service for the operating system you are using, and the commands in this article should always work.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.