本文要記述的是最簡單的Ubuntu下開機自啟 nginx的腳本
這里將nginx裝在了/usr/local/nginx目錄下,nginx本身沒有注冊成服務,所以直接使用服務開機自啟是不行的,除非自己寫nginx.service腳本,這不在本文范疇內。
創建腳本文件
$ sudo vim /etc/init.d/nginx.sh
腳本內容,注意替換root密碼、nginx執行文件目錄和配置文件目錄
#!/bin/bash
#auto run nginx when system startup
sudo -S /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf << EOF
root密碼
EOF
exit 0
指定開機自啟,最后可以添加優先級,比如,90
$ sudo update-rc.d nginx.sh defaults
此時重啟就可以發現nginx已經開機自啟了。
如果你在寫完啟動腳本的后,手動運行該腳本以確定是否可行的話,你會得到一個錯誤
insserv: warning: script 'nginx.sh' missing LSB tags and overrides
,這種錯誤不會影響腳本的啟動,只是提示腳本寫的不規范,沒有在腳本中發現以### BEGIN INIT INFO
開頭,以### END INIT INFO
結尾的標簽,因為沒有影響,這里就沒有寫,可以參考下邊的腳本去去除這個錯誤:
#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides: skeleton
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Example initscript
# Description: This file should be used to construct scripts to be
# placed in /etc/init.d. This example start a
# single forking daemon capable of writing a pid
# file. To get other behavoirs, implemend
# do_start(), do_stop() or other functions to
# override the defaults in /lib/init/init-d-script.
### END INIT INFO
# Author: Foo Bar <foobar@baz.org>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.
DESC="Description of the service"
DAEMON=/usr/sbin/daemonexecutablename