一、systemctl知識簡介
從CentOS7 Linux開始,系統里的網絡服務啟動已經從傳統的service改成了systemctl(一個systemd工具,主要負責控制systemd系統和服務管理器。),管理開機自啟動的命令也從chkconfig改為了systemctl,由systemctl一個命令代替了CentOS7以前系統中的service和chkconfig兩個命令。
系統服務的腳本也從傳統的路徑的/etc/init.d(/etc/rc.d/init.d/),改到了/usr/lib/systemd(除此之外還有/etc/systemd/system),需要自啟動運行的程序,一般存在這個系統服務目錄下,即:/usr/lib/systemd/system目錄,每一個服務以“服務名.service”結尾,該文件的內容一般分為3部分:即[Unit]、[Service]和[Install]。
二、systemctl管理的sshd服務配置介紹
下面是系統中sshd服務配置及解釋說明。
1 cat /usr/lib/systemd/system/sshd.service 2 3 [Unit] #<==對該系統服務描述及說明模塊。 4 Description=OpenSSH server daemon #<==描述性說明。 5 Documentation=man:sshd(8) man:sshd_config(5) #<==文檔列表說明。 6 After=network.target sshd-keygen.service #<==服務依賴類別說明。 7 Wants=sshd-keygen.service #<==可選的依賴服務。 8 9 [Service] #<==系統服務的運行參數設置模塊 10 Type=notify #<==服務類型,可選有forking、notify、simple等。 11 EnvironmentFile=/etc/sysconfig/sshd #<==環境變量等的配置文件。 12 ExecStart=/usr/sbin/sshd -D $OPTIONS #<==服務的啟動程序。 13 ExecReload=/bin/kill -HUP $MAINPID #<==重啟程序。 14 KillMode=process 15 Restart=on-failure 16 RestartSec=42s 17 18 [Install] #<==服務安裝的相關設置。 19 WantedBy=multi-user.target #<==這里為設置多用戶級別。可為空格分隔的列表, 表示在使用 systemctl enable 啟用此單元時, 將會在對應的目錄設置對應文件的軟連接。 20 更多說明,可參考systemd.unit、system.service文檔,此不細述,都掌握了意義也不大,可以寫出啟動腳本即可。
三、根據上面的服務配置創建nginx啟動腳本
vim /usr/lib/systemd/system/nginx.service [Unit] Description=The nginx HTTP and reverse proxy server #描述說明; After=network.target remote-fs.target nss-lookup.target #服務依賴類別說明; [Service] Type=forking #服務類型,可選有forking、notify、simple等; ExecStartPre=/application/nginx/sbin/nginx -t #啟動前檢查配置文件是否正確; ExecStart=/application/nginx/sbin/nginx #啟動nginx ExecReload=/bin/kill -s HUP $MAINPID #重載reload ExecStop=/bin/kill -s QUIT $MAINPID #停止服務 PrivateTmp=true #為服務分配獨立的空間; [Install] WantedBy=multi-user.target #多用戶級別 說明第一次啟動會提示以下報錯: [root@server nginx-1.8.1]# systemctl restart nginx.service Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units. Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details. 執行下面的命令重新載入 systemd,掃描新的或有變動的單元即可: systemctl daemon-reload #重新載入 systemd,掃描新的或有變動的單元
啟動nginx:
1 [root@lb01 ~]# systemctl daemon-reload 2 [root@lb01 ~]# systemctl stop nginx 3 [root@lb01 ~]# systemctl start nginx 4 [root@lb01 ~]# systemctl status nginx 5 ● nginx.service - The nginx HTTP and reverse proxy server 6 Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) 7 Active: active (running) since 五 2018-09-14 00:42:22 CST; 7s ago 8 Process: 1564 ExecStart=/application/nginx/sbin/nginx (code=exited, status=0/SUCCESS) 9 Process: 1563 ExecStartPre=/application/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS) 10 Main PID: 1566 (nginx) 11 CGroup: /system.slice/nginx.service 12 ├─1566 nginx: master process /application/nginx/sbin/nginx 13 └─1567 nginx: worker process 14 15 9月 14 00:42:22 lb01 systemd[1]: Starting The nginx HTTP and reverse proxy server... 16 9月 14 00:42:22 lb01 nginx[1563]: nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok 17 9月 14 00:42:22 lb01 nginx[1563]: nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful 18 9月 14 00:42:22 lb01 systemd[1]: Started The nginx HTTP and reverse proxy server. 19 [root@lb01 ~]# systemctl restart nginx 20 [root@lb01 ~]# systemctl reload nginx 21 [root@lb01 ~]# systemctl stop nginx 22 [root@lb01 ~]# systemctl restart nginx 23 [root@lb01 ~]# systemctl enable nginx 24 [root@lb01 ~]# systemctl list-unit-files|grep nginx 25 nginx.service enabled
