For advanced Linux users, starting, stopping and restarting Linux services is essential. These operations allow users to access the functionality of each service. For example, to use a web server, users need to 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 belief, starting, stopping and restarting services in Linux is relatively straightforward. We’ll be working with Linux, but all of the commands for starting, stopping and restarting Linux services can be run on CentOS, Ubuntu, Redhat, Fedora, Debian and many other distributions.
Jump to:
Understanding systemctl vs. service
There are two official management tools that provide a consistent way to start, stop, restart and manage system services in Linux:
- systemctl
- service
Which one you use will depend on whether your distribution uses systemd or init. Most modern distributions have made the switch to systemd, so systemctl is the service manager of choice. But some old habits die hard, so many administrators still hold onto the aging service command.
Fortunately, the developers of systemd made sure to retain service and redirect it to systemctl. To complicate matters more, you might find a random service you’ve installed that hasn’t been updated to either the service or systemctl tools and must manually start it with /etc/rc.d (or /etc/init.d).
So, it can still be a bit confusing.
But we’re looking for best practices here, and for starting, stopping or restarting applications on Linux, best practices begin and end with systemctl.
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 you are running the command as the root user.systemctl
manages systemd services.start
tells the systemctl command to start the Apache service.httpd
is the name of the Apache web server service.
3. Once you run the command you will get the following message:
The service httpd has started successfully.
Note that if the service is already running you will see the following message:
The service httpd is already running.
Stopping 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.
Or you may get one of the following messages:
Failed to stop service httpd. Unit httpd.service is not running.
Failed to stop service httpd. Unit httpd.service is in a failed state.
Failed to stop service httpd. Unit httpd.service is locked.
Restarting 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’ll be returned to the bash prompt.
4. You will get the following message:
The service httpd has been restarted successfully.
If the service isn’t running, you’ll see the following output:
The service httpd is not running.
You can also use the following command to check the status of the service before you try to restart it:
systemctl status httpd
Starting, stopping and restarting services with service usage
To make matters interesting, the service command still works — even for those distributions that have migrated to systemd and systemctl. This means those who instinctively type service when needing to restart a service on Linux won’t receive an “Unknown command” error.
In the case of service, the command will redirect to systemctl. In fact, when you run the service command on a systemctl-enabled distribution, you’ll clearly see the redirect information (Figure A).
Figure A
The service command usage is a bit different from 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’ll see service redirected to systemctl, but the service you are attempting to start, stop or restart will succeed.
It’s not that complicated
I’ve complicated the issue by mentioning the old methods of starting, stopping and restarting services in Linux. Truth be told, the process of controlling services in Linux isn’t that complex; you only have to understand the right command to use.
To learn more about what systemctl can do for you, make sure to issue the command man systemctl and give the man page a read.
Source of Article