CentOS 7 /RHEL7: Start/Stop/Restart A Service
How Do I start a service unit under centos 7/RHEL 7 operating system? How can I stop a system service? How to restart a server in the latest centos 7 or RHEL 7?
In old centos or rhel system, you should know that you can use “service” command or directly run “/etc/init.d/<service_name> start/stop/restart” to start/stop/restart a service. but in centos 7 or RHEL 7, you need to use “systemctl” command to start/stop/restart service instead of “service”.
CentOS 7 /RHEL7 Start/Stop/Restart A Service
CentOS 7 Start A service
To start a service in your current system by running the following command as root:
systemctl start <name>.service
For example start a apache httpd service, the unit name of this service is: httpd.service, enter:
$ sudo systemctl start httpd.service
Now checking the status of httpd service if it is started, enter:
$ sudo systemctl status httpd.service httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: active (running) since Fri 2014-12-12 07:00:36 CET; 2min 46s ago Main PID: 30133 (httpd) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─30133 /usr/sbin/httpd -DFOREGROUND ├─30135 /usr/sbin/httpd -DFOREGROUND ├─30136 /usr/sbin/httpd -DFOREGROUND ├─30137 /usr/sbin/httpd -DFOREGROUND ├─30138 /usr/sbin/httpd -DFOREGROUND └─30139 /usr/sbin/httpd -DFOREGROUND Dec 12 07:00:36 devops systemd[1]: Starting The Apache HTTP Server... Dec 12 07:00:36 devops httpd[30133]: AH00557: httpd: apr_sockaddr_info_get(...ps Dec 12 07:00:36 devops httpd[30133]: AH00558: httpd: Could not reliably det...ge Dec 12 07:00:36 devops systemd[1]: Started The Apache HTTP Server. Hint: Some lines were ellipsized, use -l to show in full.
CentOS 7 Stop A Service
To stop a service in your centos 7 system, you need to use “systemctl” command with “stop” option and appending the corresponding service unit, enter:
systemctl stop <service_name>.service
For example stop a service named “httpd.service” and also check its status if it is stopped, run the following command:
$ sudo systemctl stop httpd.service $ sudo systemctl status httpd.service
Outputs:
[root@devops /]$ sudo systemctl stop httpd.service [root@devops /]$ sudo systemctl status httpd.service httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled) Active: inactive (dead) Dec 12 04:37:57 devops httpd[27774]: AH00558: httpd: Could not reliably det...ge Dec 12 04:37:57 devops systemd[1]: Started The Apache HTTP Server. Dec 12 06:59:54 devops systemd[1]: Stopping The Apache HTTP Server... Dec 12 06:59:55 devops systemd[1]: Stopped The Apache HTTP Server. Dec 12 07:00:36 devops systemd[1]: Starting The Apache HTTP Server... Dec 12 07:00:36 devops httpd[30133]: AH00557: httpd: apr_sockaddr_info_get(...ps Dec 12 07:00:36 devops httpd[30133]: AH00558: httpd: Could not reliably det...ge Dec 12 07:00:36 devops systemd[1]: Started The Apache HTTP Server. Dec 12 07:09:17 devops systemd[1]: Stopping The Apache HTTP Server... Dec 12 07:09:18 devops systemd[1]: Stopped The Apache HTTP Server. Hint: Some lines were ellipsized, use -l to show in full.
CentOS 7 Restart A service
If you want to restart a service in your system, you can use “retart” option to “systemctl” command, this command will stop the service firstly, then start this service again, type:
$sudo systemctl restart <service_name>.service
To restart A service named httpd.service, type:
$sudo systemctl restart httpd.service
To only reload the configuration of one service without interrupting their execution, issue the following command to reload a service:
$ sudo systemctl reload <service_name>.service.
Example:reloading a service named as “httpd.service”
$ sudo systemctl reload httpd.service
done..