1. 程式人生 > 其它 >使用systemctl管理服務(nginx)

使用systemctl管理服務(nginx)

首先調整好路徑資訊,修改配置檔案
vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
ExecQuit=/usr/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

由於程序已經存在,編輯好之後,執行啟動命令是失敗的。
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2021-11-12 09:15:54 CST; 51s ago
Process: 2378 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Process: 2376 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)

Nov 12 09:15:53 mcw1 nginx[2378]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:15:53 mcw1 nginx[2378]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:15:53 mcw1 nginx[2378]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:15:54 mcw1 nginx[2378]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:15:54 mcw1 nginx[2378]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:15:54 mcw1 systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 12 09:15:54 mcw1 nginx[2378]: nginx: [emerg] still could not bind()
Nov 12 09:15:54 mcw1 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Nov 12 09:15:54 mcw1 systemd[1]: Unit nginx.service entered failed state.
Nov 12 09:15:54 mcw1 systemd[1]: nginx.service failed.

手動執行檢查命令,發現是被佔用的,也就是由於nginx已經是啟動狀態,無論怎麼弄都不行
[root@mcw1 ~]# /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

把nginx服務停掉
[root@mcw1 ~]# ps -ef|grep nginx
root 1925 1 0 08:33 ? 00:00:00 nginx: master process nginx
nginx 2220 1925 0 09:10 ? 00:00:00 nginx: worker process
root 2411 1829 0 09:19 pts/0 00:00:00 grep --color=auto nginx
[root@mcw1 ~]# kill 1925
[root@mcw1 ~]# ps -ef|grep nginx
root 2413 1829 0 09:19 pts/0 00:00:00 grep --color=auto nginx
[root@mcw1 ~]#

用systemctl啟動nginx
[root@mcw1 ~]# systemctl start nginx

這時檢視程序是沒有問題的
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-11-12 09:20:03 CST; 3s ago
Process: 2511 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Process: 2510 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2514 (nginx)
CGroup: /system.slice/nginx.service
├─2514 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─2515 nginx: worker process

Nov 12 09:20:03 mcw1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 12 09:20:03 mcw1 systemd[1]: Started The nginx HTTP and reverse proxy server.

用systemctl停掉nginx,也米有問題
[root@mcw1 ~]# systemctl stop nginx
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)

Nov 12 09:17:01 mcw1 systemd[1]: nginx.service failed.
Nov 12 09:20:03 mcw1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 12 09:20:03 mcw1 systemd[1]: Started The nginx HTTP and reverse proxy server.
Nov 12 09:20:23 mcw1 systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Nov 12 09:20:23 mcw1 systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Nov 12 09:20:23 mcw1 systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Unknown lvalue 'ExecQuit' in section 'Service'
Nov 12 09:20:23 mcw1 systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Unknown lvalue 'ExecQuit' in section 'Service'
Nov 12 09:20:26 mcw1 systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Unknown lvalue 'ExecQuit' in section 'Service'

用systemctl啟動和重啟nginx,都米有問題
[root@mcw1 ~]# systemctl start nginx
[root@mcw1 ~]# systemctl restart nginx
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-11-12 09:20:38 CST; 4s ago
Process: 2548 ExecStop=/usr/sbin/nginx -s stop (code=exited, status=0/SUCCESS)
Process: 2554 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Process: 2553 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2556 (nginx)
CGroup: /system.slice/nginx.service
├─2556 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─2557 nginx: worker process

Nov 12 09:20:38 mcw1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 12 09:20:38 mcw1 nginx[2553]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 12 09:20:38 mcw1 nginx[2553]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 12 09:20:38 mcw1 systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@mcw1 ~]#


用systemctl停掉nginx。然後直接用命令啟動nginx。再使用systemctl重啟管理,這下子就不行了。
[root@mcw1 ~]# systemctl stop nginx.service
[root@mcw1 ~]# nginx
[root@mcw1 ~]# systemctl restart nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@mcw1 ~]#

總結:也就是如果用systemtl管理的話,後面也用systemctl就行,直接用命令啟停服務,也可以。但是兩者交叉管理服務,很有可能出問題

根據報錯提示檢視錯誤詳情。顯示有地址已經在使用的錯誤資訊,說明服務已經啟動了,但是命令直接啟動的,沒有用systemctl啟動,所以後面的檢視,啟動,重啟,等操作用systemctl會出錯。
[root@mcw1 ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2021-11-12 09:28:14 CST; 3min 1s ago
Process: 2712 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Process: 2711 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)

Nov 12 09:28:13 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:13 mcw1 nginx[2712]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:28:13 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] still could not bind()
Nov 12 09:28:14 mcw1 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Nov 12 09:28:14 mcw1 systemd[1]: Unit nginx.service entered failed state.
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service failed.
[root@mcw1 ~]# journalctl -xe
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 polkitd[902]: Unregistered Authentication Agent for unix-process:2705:524584 (system bus name :1.122, object path /org/freedesktop/PolicyK
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] still could not bind()
Nov 12 09:28:14 mcw1 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
Nov 12 09:28:14 mcw1 systemd[1]: Unit nginx.service entered failed state.
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service failed.
Nov 12 09:30:01 mcw1 systemd[1]: Started Session 23 of user root.
-- Subject: Unit session-23.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-23.scope has finished starting up.
--
-- The start-up result is done.
Nov 12 09:30:01 mcw1 systemd[1]: Starting Session 23 of user root.
-- Subject: Unit session-23.scope has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-23.scope has begun starting up.
Nov 12 09:30:01 mcw1 CROND[2718]: (root) CMD (/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1)