用start-stop-daemon啟動Nginx

來源:互聯網
上載者:User

在前面學習Ubuntu apt-get install nginx 建立的nginx啟動指令碼中,看到start-stop-daemon的用法。

迅速查了一下手冊(用man start-stop-daemon)。這個程式用來啟動和關閉系統層級的進程。

下面我用該命令啟動我自己編譯的nginx程式:

start-stop-daemon --start --quiet --make-pidfile --pidfile /opt/nginx.pid --exec /home/chenshu/nginx/bin/nginx  -- -c /home/chenshu/nginx/etc_nginx/nginx.conf 

注意,這裡都使用絕對路徑,相對路徑會有問題。

--pidfile 指定了儲存進程id的檔案 /opt/nginx.pid--exec 指定了可執行程式-- 之後加上傳遞給nginx的參數 -c /home/chenshu/nginx/etc_nginx/nginx.conf

--make-pidfile  使用 是因為有時候start-stop-daemon 不產生pidfile

單獨運行nginx的時候,還可以指定 -t參數用於測試-c 參數指定的設定檔是否正確。

由於我要將所有的nginx程式,日誌,配置都放在/home/dist/carrier/nginx目錄下,因此對應的修改啟動指令碼/etc/init.d/nginx

由於沒有找到辦法指定error log的位置. 因此現在繞過的方法是 編譯nginx的時候使用--prefix=/home/dist/carrier/nginx參數。

這裡用到了之前沒有用的DAEMON_OPTS變數,傳遞-c參數。注意,必須用雙引號。

#!/bin/sh### BEGIN INIT INFO# Provides:          nginx# Required-Start:    $local_fs $remote_fs $network $syslog# Required-Stop:     $local_fs $remote_fs $network $syslog# Default-Start:     2 3 4 5# Default-Stop:      0 1 6# Short-Description: starts the nginx web server# Description:       starts nginx using start-stop-daemon### END INIT INFOPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binCARRIER_HOME=/home/dist/carrierDAEMON=$CARRIER_HOME/nginx/bin/nginxPID_FILE=$CARRIER_HOME/nginx/nginx.pidCONFIG_FILE=$CARRIER_HOME/nginx/conf/nginx.confDAEMON_OPTS="-c $CONFIG_FILE"NAME=nginxDESC=nginx# Include nginx defaults if availableif [ -f /etc/default/nginx ]; then. /etc/default/nginxfitest -x $DAEMON || exit 0set -e. /lib/lsb/init-functionstest_nginx_config() {if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; thenreturn 0else$DAEMON -t $DAEMON_OPTSreturn $?fi}case "$1" instart)echo -n "Starting $DESC: "test_nginx_config# Check if the ULIMIT is set in /etc/default/nginxif [ -n "$ULIMIT" ]; then# Set the ulimitsulimit $ULIMITfistart-stop-daemon --start --quiet --make-pidfile --pidfile $PID_FILE --exec $DAEMON -- $DAEMON_OPTS || trueecho "$NAME.";;stop)echo -n "Stopping $DESC: "start-stop-daemon --stop --quiet --pidfile $PID_FILE --exec $DAEMON || trueecho "$NAME.";;restart|force-reload)echo -n "Restarting $DESC: "start-stop-daemon --stop --quiet --pidfile $PID_FILE --exec $DAEMON || truesleep 1test_nginx_config# Check if the ULIMIT is set in /etc/default/nginxif [ -n "$ULIMIT" ]; then# Set the ulimitsulimit $ULIMITfistart-stop-daemon --start --quiet --make-pidfile --pidfile $PID_FILE --exec $DAEMON -- $DAEMON_OPTS || trueecho "$NAME.";;reload)echo -n "Reloading $DESC configuration: "test_nginx_configstart-stop-daemon --stop --signal HUP --quiet --pidfile $PID_FILE --exec $DAEMON || trueecho "$NAME.";;configtest|testconfig)echo -n "Testing $DESC configuration: "if test_nginx_config; thenecho "$NAME."elseexit $?fi;;status)status_of_proc -p $PID_FILE "$DAEMON" nginx && exit 0 || exit $?;;*)echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2exit 1;;esacexit 0

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.