centos 7 x編寫開機啟動服務

2022-08-18 17:45:15 字數 3467 閱讀 2700

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

vi /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 quit

privatetmp=true

[install]

wantedby=multi-user.target

[unit]

description=uwsgi

after=network.target

[service]

type=forking

execstart=/usr/bin/uwsgi --ini /usr/local/nginx/uwsgi_temp/uwsgi.ini

execstop=/usr/bin/uwsgi --stop /usr/local/nginx/uwsgi_temp/uwsgi.pid

execreload=/usr/bin/uwsgi --reload uwsgi.pid

privatetmp=true

[install]

wantedby=multi-user.target

description:描述服務

after:描述服務類別

[service]服務執行引數的設定

type=forking是後台執行的形式

execstart為服務的具體執行命令

execreload為重啟命令

execstop為停止命令

privatetmp=true表示給服務分配獨立的臨時空間

注意:[service]的啟動、重啟、停止命令全部要求使用絕對路徑

[install]執行級別下服務安裝的相關設定,可設定為多使用者,即系統執行級別為3

儲存退出。

設定開機自啟動

systemctl enable nginx.service

啟動nginx服務

systemctl start nginx.service 

停止開機自啟動

systemctl disable nginx.service

檢視服務當前狀態

systemctl status nginx.service

重新啟動服務

systemctl restart nginx.service 

檢視所有已啟動的服務

systemctl list-units --type=service

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)

改變至多使用者執行級別。

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。

3runlevel3.target, multi-user.target

多使用者,非圖形化。使用者可以通過多個控制台或網路登入。

5runlevel5.target, graphical.target

多使用者,圖形化。通常為所有執行級別 3 的服務外加圖形化登入。

6runlevel6.target, reboot.target

重啟emergency

emergency.target

緊急 shell

centos7 x設定nginx開機自啟動

1 在 lib systemd system 下新建nginx.service檔案,內容如下 unit description nginx service after network.target service type forking execstart sbin nginx execreloa...

Centos7 x開機自啟動指令碼

1 因為在centos7中 etc rc.d rc.local的許可權被降低了,所以需要賦予其可執行權 chmod x etc rc.d rc.local2 賦予指令碼可執行許可權 假設 usr local script autostart.sh是你的指令碼路徑,給予執行許可權 chmod x us...

centOS 7 X 新增開機自動啟動指令碼

linux設定服務開機自動啟動的方式有好多種,這裡介紹一下通過chkconfig命令新增指令碼為開機自動啟動的方法。編寫指令碼autostart.sh 這裡以開機啟動redis服務為例 指令碼內容如下 bin sh chkconfig 2345 80 90 description 開機自動啟動的指令...