centos 7.x編寫開機啟動服務

來源:互聯網
上載者:User

標籤:

centos 7以上是用Systemd進行系統初始化的,Systemd 是 Linux 系統中最新的初始化系統(init),它主要的設計目標是克服 sysvinit 固有的缺點,提高系統的啟動速度。關於Systemd的詳情介紹在這裡。

Systemd服務檔案以.service結尾,比如現在要建立nginx為開機啟動,如果用yum install命令安裝的,yum命令會自動建立nginx.service檔案,直接用命令

systemcel enable nginx.service

設定開機啟動即可。
在這裡我是用源碼編譯安裝的,所以要手動建立nginx.service服務檔案。
開機沒有登陸情況下就能啟動並執行程式,存在系統服務(system)裡,即:

/lib/systemd/system/

1.在系統服務類別目錄裡建立nginx.service檔案

vim /lib/systemd/system/nginx.service

內容如下

[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s quitPrivateTmp=true [Install] WantedBy=multi-user.target

[Unit]:服務的說明
Description:描述服務
After:描述服務類別
[Service]服務運行參數的設定
Type=forking是後台啟動並執行形式
ExecStart為服務的具體運行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務分配獨立的臨時空間
注意:[Service]的啟動、重啟、停止命令全部要求使用絕對路徑
[Install]運行層級下服務安裝的相關設定,可設定為多使用者,即系統運行層級為3

儲存退出。

2.設定開機啟動

systemctl enable nginx.service

3.nginx命令
啟動nginx服務

systemctl start nginx.service

設定開機自啟動

systemctl enable nginx.service

停止開機自啟動

systemctl disable nginx.service

查看服務目前狀態

systemctl status nginx.service

重新啟動服務

systemctl restart nginx.service查看所有已啟動的服務
systemctl list-units --type=service

4.Systemd 命令和 sysvinit 命令的對照表

Sysvinit 命令 Systemd 命令 備忘
service foo start systemctl start foo.service 用來啟動一個服務 (並不會重啟現有的)
service foo stop systemctl stop foo.service 用來停止一個服務 (並不會重啟現有的)。
service foo restart systemctl restart foo.service 用來停止並啟動一個服務。
service foo reload systemctl reload foo.service 當支援時,重新裝載設定檔而不中斷等待操作。
service foo condrestart systemctl condrestart foo.service 如果服務正在運行那麼重啟它。
service foo status systemctl status foo.service 彙報服務是否正在運行。
ls /etc/rc.d/init.d/ systemctl list-unit-files --type=service 用來列出可以啟動或停止的服務列表。
chkconfig foo on systemctl enable foo.service 在下次啟動時或滿足其他觸發條件時設定服務為啟用
chkconfig foo off systemctl disable foo.service 在下次啟動時或滿足其他觸發條件時設定服務為禁用
chkconfig foo systemctl is-enabled foo.service 用來檢查一個服務在當前環境下被配置為啟用還是禁用。
chkconfig –list systemctl list-unit-files --type=service 輸出在各個運行層級下服務的啟用和禁用情況
chkconfig foo –list ls /etc/systemd/system/*.wants/foo.service 用來列出該服務在哪些運行層級下啟用和禁用。
chkconfig foo –add systemctl daemon-reload 當您建立新服務檔案或者變更設定時使用。
telinit 3 systemctl isolate multi-user.target (OR systemctl isolate runlevel3.target OR telinit 3) 改變至多使用者運行層級。

5.Sysvinit 運行層級和 systemd 目標的對應表

Sysvinit 運行層級 Systemd 目標 備忘
0 runlevel0.target, poweroff.target 關閉系統。
1, s, single runlevel1.target, rescue.target 單一使用者模式。
2, 4 runlevel2.target, runlevel4.target, multi-user.target 使用者定義/域特定運行層級。預設等同於 3。
3 runlevel3.target, multi-user.target 多使用者,非圖形化。使用者可以通過多個控制台或網路登入。
5 runlevel5.target, graphical.target 多使用者,圖形化。通常為所有運行層級 3 的服務外加圖形化登入。
6 runlevel6.target, reboot.target 重啟
emergency emergency.target 緊急 Shell

 

原文地址http://www.dohooe.com/2016/03/03/352.html

 

centos 7.x編寫開機啟動服務

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.