The ps command can be used to determine if your system is using init or systemd. If PID 1 is systemd, then you will use systemctl to start, stop, enable, disable, and check the status of a systemd service.
ps -ef | head UID PID PPID C STIME TTY TIME CMD root 1 0 0 Mar01 ? 00:06:47 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
If PID 1 is init, then you will use the chkconfig command.
UID PID PPID C STIME TTY TIME CMD root 1 0 0 Mar08 ? 00:00:01 /sbin/init
The service command is used by systems using init. systemctl is used by systems using systemd.
When issuing a systemctl command, if the following is displayed, this means that the service file was updated, such as /etc/systemd/system/example.service.
Warning: example.service changed on disk. Run 'systemctl daemon-reload' to reload units.
As the warning suggested, the systemctl daemon-reload command can be used to resolve this issue.
The systemctl status command will show if a service is running. In this example, systemctl is used to check the status of the nginx service.
systemctl status nginx
Following is an example of what will be displayed. In this example, nginx is active.
nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) Active: active (running) since Wed 2020-03-04 00:00:17 CST; 3 weeks 4 days ago Process: 1097 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS) Process: 1034 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS) Process: 1026 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS) Main PID: 1123 (nginx) CGroup: /system.slice/nginx.service â”œâ”€1123 nginx: master process /usr/sbin/nginx â”œâ”€1137 nginx: worker process â””â”€1138 nginx: worker process Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
In this example, nginx is inactive.
systemctl status example.service . . . example.service - Example Service Loaded: loaded (/etc/systemd/system/example.service; disabled; vendor preset: disabled) Active: inactive (dead)
The systemctl start command is used to start a service.
systemctl start example.service
If the service failed the start, something like this will be display. As this message suggested, the systemctl status command can be used to start understanding why the service failed to start.
Job for example.service failed because the control process exited with error code. See "systemctl status example.service" and "journalctl -xe" for details.
The systemctl stop command is used to stop a service. The stop command will not return any output. Typically, after issuing the stop command, the status command is used to ensure the status of the service is inactive.
systemctl stop example.service
The systemctl get-default command is used to determine the target for the entire system. In this example, the entire system is in graphical.target.
systemctl get-default . . . multi-user.target
The systemctl set-default command is used to set the target. In this example, the target is set to graphical.target.
systemctl set-default graphical.target