CentOS 7 上systemctl 的用法 【轉】
阿新 • • 發佈:2019-02-20
CentOS 7設定開機啟動服務,新增自定義系統服務
- 建立服務檔案
- 儲存目錄
- 設定開機自啟動
- 其他命令
1.建立服務檔案
檔案路徑
vim /usr/lib/systemd/system/nginx.service
- 1
- 1
服務檔案內容
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
檔案內容解釋
[Unit]:服務的說明
Description:描述服務
After:描述服務類別
[Service]服務執行引數的設定
Type=forking是後臺執行的形式
ExecStart為服務的具體執行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:啟動、重啟、停止命令全部要求使用絕對路徑
[Install]服務安裝的相關設定,可設定為多使用者
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2.儲存目錄
以754的許可權儲存在目錄:
/usr/lib/systemd/system
- 1
- 1
3.設定開機自啟動
任意目錄下執行
systemctl enable nginx.service
- 1
- 1
4.其他命令
啟動nginx服務
systemctl start nginx.service
- 1
- 1
設定開機自啟動
systemctl enable nginx.service
- 1
- 1
停止開機自啟動
systemctl disable nginx.service
- 1
- 1
檢視服務當前狀態
systemctl status nginx.service
- 1
- 1
重新啟動服務
systemctl restart nginx.service
- 1
- 1
檢視所有已啟動的服務
systemctl list-units --type=service
- 1
- 1
systemctl is-enabled servicename.service #查詢服務是否開機啟動
systemctl enable *.service #開機執行服務
systemctl disable *.service #取消開機執行
systemctl start *.service #啟動服務
systemctl stop *.service #停止服務
systemctl restart *.service #重啟服務
systemctl reload *.service #重新載入服務配置檔案
systemctl status *.service #查詢服務執行狀態
systemctl --failed #顯示啟動失敗的服務
注:*代表某個服務的名字,如http的服務名為httpd