Nginx shell系統自啟服務指令碼編寫

2021-10-04 17:19:02 字數 2323 閱讀 1003

由於每次重啟伺服器後,nginx都需要手動啟動,相當不便,所以寫了個自啟指令碼

建立nginx指令碼

cd /etc/init.d

vim nginx

#!/bin/sh

exec=/home/soft/nginx/sbin/nginx

pidfile=/home/soft/nginx/logs/nginx.pid

conf=

"/home/soft/nginx/conf/nginx.conf"

case

"$1"

in start)

if[ -f $pidfile

]then

echo

"$pidfile exists,process is already running or crashed"

else

echo

"starting nginx server..."

$execfi;

; stop)if[

! -f $pidfile

]then

echo

"$pidfile does not exists,process is not running"

else

pid=

$(cat $pidfile)

echo

"stopping..."

$exec -s quit

while

[ -x /proc/$]do

echo

"waiting for nginx to shutdown ..."

sleep 1

done

echo

"nginx stopped"fi;

; reload)if[

! -f $pidfile

]then

echo

"$pidfile does not exists,process is not running"

else

echo

"nginx reload..."

$exec -s reload

echo

$(ps -ef |

grep nginx |

grep $exec |

grep -v grep

|awk'')

fi;;

*)echo

"please use start or stop or reload as first argument";;

esac

增加許可權

chmod a+x /etc/init.d/nginx

啟用指令碼

1、開啟nginx開機自啟,此項需要第二步操作

chkconfig nginx on

2、啟動nginx

service nginx start

3、優雅停止nginx

service nginx stop

4、優雅的重新載入

service nignx reload

第二步是在/lib/systemd/system/目錄下加入nginx.service

1、cd /lib/systemd/system/

2、vim nginx.service

[unit]

description=nginx.service

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

1、開機自啟動

systemctl enable nginx

2、啟動

systemctl start nginx

3、停止

systemctl stop nginx

4、載入

systemctl reload nginx

consul設定自啟

在conusl.exe所在資料夾中新建乙個consul.bat檔案 在consul.bat中編寫consul agent dev 建乙個檔案script.vbs 裡面寫set ws wscript.createobject wscript.shell ws.run 此為你的consul.bat檔案的...

tomcat掛了自啟

先上指令碼 bin sh function 自動監控tomcat程序,掛了就執行重啟操作 author wangqy define 獲取tomcat ppid tomcatid ps ef grep tomcat grep w apache grep v grep awk tomcat startu...

phpstudy 開機自啟

phpstudy每次開機就要重啟一次,單單吧phpstudy面板加入自啟動目錄毫無意義,還是需要點選啟動才能執行。所以考慮如何自啟。其實主要就是apache和資料庫都自動啟動即可 因此主要包含兩部分 第一部分apach的自啟動 進入到自己的phpstudy的apache下的bin目錄,在命令列輸入h...