This post will show you how to start or stop a service on a RHEL or CentOS 7 server.
Check the state of the service on CentOS 7
To check the state, run systemctl status
on your target service.
$ systemctl status httpd httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled) Active: active (running) since Fri 2015-07-31 22:26:29 CEST; 1 weeks 0 days ago Main PID: 27482 (httpd) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─ 1885 /usr/sbin/httpd -DFOREGROUND ├─ 1886 /usr/sbin/httpd -DFOREGROUND ...
There’s quite a bit of output in systemctl
, so let’s break it down.
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
The enabled/disabled at the end tells you if the service is enabled/disabled to start on boot.
Active: active (running) since Fri 2015-07-31 22:26:29 CEST; 1 weeks 0 days ago
The Active: output tells you the state of the service. It’s actively running and was started 1 week ago.
Main PID: 27482 (httpd)
This one explains itself, the main PID is 27482. This is the Apache2 webserver, so we’ll see many child processes with Parent ID referring to this Main PID.
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
The Status: output isn’t available for every service. In the case of a webserver, it can show you throughput of the HTTP calls.
In the case of the MariaDB 10.0 service, it’ll just show you the state of the service. Nothing more.
$ systemctl status mysql mysql.service - LSB: start and stop MySQL Loaded: loaded (/etc/rc.d/init.d/mysql) Active: active (running) since Fri 2015-07-31 20:42:52 CEST; 1 weeks 1 days ago CGroup: /system.slice/mysql.service
The last part of the systemctl status
output shows you the last lines of logs from that particular service.
Stop a service on CentOS 7
You stop a service with the systemctl stop
command.
$ systemctl stop httpd
There’s no additional output, you can still use systemctl status
to verify the service stopped.
$ systemctl status httpd ... Active: inactive (dead) since Sat 2015-08-08 20:53:23 CEST; 25s ago Process: 28234 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS) Main PID: 27482 (code=exited, status=0/SUCCESS)
The service is “inactive (dead)” and was cleanly shutdown (“code=exited, status=0”).
Alternatively, if you kill -9
a process, it’ll show you that in the systemctl status
output.
$ systemctl status httpd ... Process: 28465 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=killed, signal=KILL) Main PID: 28465 (code=killed, signal=KILL)
Useful output right there.
Start a service on CentOS 7
Like stopping a service, you can start a service with systemctl start
.
$ systemctl start httpd
Again, no output, unless something went wrong. Use systemctl status
to check the status of your service.
If you made an error in your service configuration, you get output like this.
$ systemctl start httpd Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
To see why the service failed to start, check the specific service logs. Systemd also has a way to output the info, but I find it cumbersome and sometimes lacking info – the kind of info that’s logged in additional error logs from the particular service.
$ systemctl status httpd ... Aug 08 20:57:38 ma httpd[29986]: AH00526: Syntax error on line 14 of /etc/httpd/conf.d/something.conf: Aug 08 20:57:38 ma httpd[29986]: Invalid command 'syntax', perhaps misspelled or defined by a module not included in the server configuration
If you get an error, fix it and try to start your service again.