For superior Linux customers, beginning, stopping, and restarting Linux companies is crucial. These operations enable customers to entry the options of every service. For instance, to make use of an internet server, customers want to begin the Apache service, or to make use of a database, customers want to begin the MySQL service. Linux service administration can also be vital for system stability and may also help enhance system efficiency.
Despite widespread perception, beginning, stopping, and restarting companies in Linux is comparatively easy. We will work with Linux, however all instructions for beginning, stopping and restarting Linux companies could be run on CentOS, Ubuntu, Redhat, Fedora, Debian and lots of different distributions.
What is the distinction between the systemctl and repair instructions?
There are two official administration instruments that present a constant strategy to begin, cease, restart, and handle system companies in Linux:
Systemctl gives extra superior options, together with dependency administration, enabling/disabling companies, and integration with journalctl for logging. The service is easier and is usually used for primary service begin, cease, and standing instructions. It is usually used with older SysVinit-based programs.
Which one you utilize will rely on whether or not your distribution makes use of systemd or init. Most trendy distributions now use systemd, so systemctl is the popular service supervisor. But some outdated habits die laborious, which is why many directors proceed to keep up command of the now out of date service.
Luckily, the systemd builders made positive to maintain the service and redirect it to systemctl, so even on systemd-based programs, utilizing the service will nonetheless work for primary duties
To complicate issues additional, it’s possible you’ll discover a random service you will have put in that has not up to date both the service or the systemctl instruments, and it’s important to begin it manually with /and so forth/rc.d (or /and so forth/init.d). But right here we’re in search of finest practices and to begin, cease or restart functions on Linux, finest practices begin and finish with systemctl.
SEE: Start studying Linux for IT and Sysadmin with this package deal
Starting a Linux service
Let’s say you need to begin the Apache server.
To do that:
- Open a terminal window.
- Run the command
sudo systemctl begin httpd
.
In this command:
sudo
tells Linux that you’re working the command as the basis person.systemctl
manages systemd companies.begin
tells the systemctl command to begin the Apache service.httpd
is the identify of the Apache internet server service.
- Once the command is executed you’ll obtain the next message:
The service httpd has began efficiently.
Note that if the service is already working the next message can be displayed:
The service httpd is already working.
SEE: How to Quickly Open a Terminal in a Specific Linux Directory
Common error messages
Failed to begin httpd.service. Unit httpd.service not discovered.
This error happens if the Apache internet server package deal is just not put in or the service unit file is lacking. Install the Apache package deal utilizing sudo apt set up apache2 (on Debian-based programs) or sudo yum set up httpd (on Red Hat-based programs) to repair it.
Failed to begin httpd.service. Address already in use.
This signifies that one other course of is already utilizing the port that Apache desires to hook up with (often port 80). Identify the method in battle with sudo lsof -i:80
and cease it or change the port configuration within the Apache configuration file.
Stopping a Linux service
To cease the Apache service:
- Open a terminal window
- Run the command
sudo systemctl cease httpd
. - You ought to now see the next message:
The service httpd has been stopped efficiently.
Note that if the service, on this case Apache, was not working, you’ll obtain the next message:
Failed to cease service httpd. Unit httpd.service is just not loaded.
Install it utilizing sudo apt set up apache2
(based mostly on Debian) or sudo yum set up httpd
(Based on Red Hat).
Or it’s possible you’ll obtain one of many following messages:
Failed to cease service httpd. Unit httpd.service is just not working.
This signifies that Apache has already been stopped, so no motion is required.
Failed to cease service httpd. Unit httpd.service is in a failed state.
This means that Apache has encountered an error and is in a failed state. To repair the issue, run sudo journalctl -xe
to view detailed logs, then strive restarting the service.
Failed to cease service httpd. Unit httpd.service is locked.
This error happens if one other course of controls the service. Please wait briefly and check out once more or verify if there’s any administration exercise in progress with ps aux | grep httpd
to establish the blocking course of.
SEE: Linux 101: How to seek for recordsdata from the Linux command line
Restarting a Linux service
To restart the identical service (Apache):
- Open a terminal window.
- Run the command
sudo systemctl restart httpd
. - The service will restart and you can be returned to the bash immediate.
- You will obtain the next message:
The service httpd has been restarted efficiently.
Common error messages
If the Apache service is just not working, you will notice the next output:
The service httpd is just not working.
You can begin it immediately with sudo systemctl begin httpd
or verify the standing with systemctl standing httpd
.
You may see the next:
Job for httpd.service failed.
This usually signifies a configuration or dependency challenge. To resolve this challenge, overview the error particulars with sudo journalctl -xe
and proper any configuration issues.
Starting, stopping, and restarting companies utilizing the service
To make issues attention-grabbing, the service command nonetheless works, even for these distributions which have switched to systemd and systemctl. This signifies that those that instinctively sort service when they should restart a service on Linux won’t obtain a file Unknown command
mistake.
SEE: Run a Google search from the Linux command line with Googler
In the case of service, the command will redirect to systemctl. In truth, while you run the service command on a systemctl-enabled distribution, you’ll clearly see the redirection data.
Using the service command is barely completely different from systemctl. The service identify and begin, cease, and restart choices are modified:
sudo service httpd begin
sudo service httpd cease
sudo service httpd restart
In any case, you will notice the service redirected to systemctlhowever the service you are attempting to begin, cease, or restart will succeed.
To be taught extra about what systemctl can do for you, make sure you run the person systemctl command and browse the person web page.