Debian

Nginx init.d 腳本

  • February 1, 2016

昨天在我的 debian 機器上進行了 dist 升級後,我的 nginx 初始化腳本出現了一些問題。每當我嘗試使用 init 腳本啟動 nginx 時,它都會出現一條錯誤消息:

# service nginx start
Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.

systemctl status nginx.service 的輸出:

# systemctl status nginx.service
● nginx.service - A high performance web server and a reverse proxy server
  Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
  Active: failed (Result: exit-code) since Tue 2015-05-19 12:36:12 CEST; 7s ago
 Process: 14087 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Process: 14126 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=203/EXEC)

journalctl -xn 的輸出:

# journalctl -xn
systemd[1]: nginx.service never wrote its PID file. Failing.
systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
systemd[1]: Unit nginx.service entered failed state.
systemd[14126]: Failed at step EXEC spawning /usr/sbin/nginx: No such file or directory
-- Subject: Process /usr/sbin/nginx could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- The process /usr/sbin/nginx could not be executed and failed.
--
-- The error number returned while executing this process is 2.
systemd[1]: nginx.service: control process exited, code=exited status=203
systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
systemd[1]: Unit nginx.service entered failed state.

我在 init.d 中的 nginx 文件:

#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# 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 INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
   . /etc/default/nginx
fi

set -e

. /lib/lsb/init-functions

case "$1" in
 start)
   echo -n "Starting $DESC: "
   start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
       --exec $DAEMON -- $DAEMON_OPTS || true
   echo "$NAME."
   ;;
 stop)
   echo -n "Stopping $DESC: "
   start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
       --exec $DAEMON || true
   echo "$NAME."
   ;;
 restart|force-reload)
   echo -n "Restarting $DESC: "
   start-stop-daemon --stop --quiet --pidfile \
       /usr/local/nginx/logs/$NAME.pid --exec $DAEMON || true
   sleep 1
   start-stop-daemon --start --quiet --pidfile \
       /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
   echo "$NAME."
   ;;
 reload)
     echo -n "Reloading $DESC configuration: "
     start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
         --exec $DAEMON || true
     echo "$NAME."
     ;;
 status)
     status_of_proc -p /usr/local/nginx/logs/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
     ;;
 *)
   N=/etc/init.d/$NAME
   echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
   exit 1
   ;;
esac

exit 0

nginx 守護程序文件位於/usr/local/sbin/nginx(我在那裡編譯它)。

到目前為止我嘗試過的事情:我將守護程序文件複製/usr/local/sbin/nginx/usr/sbin/nginx

當我使用service nginx start它時,它會掛起啟動 nginx。當我嘗試連接到埠 80 時,它確實有效。所以我繼續編輯我的初始化腳本並更改PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binPATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/sbin:/usr/bin. 然而,這給出了相同的結果。

到目前為止我還沒有嘗試過其他任何東西。

我在 init.d 中的 nginx 文件:

……完全無關緊要。看看輸出systemctl。它準確地告訴您要查看的文件:

已載入:已載入(**/lib/systemd/system/nginx.service**;已啟用)

是你的服務單位。並systemctl告訴您該服務單元為準備啟動服務而配置了什麼:

程序:14126 ExecStartPre= **/usr/sbin/nginx -t -q -g 守護程序開啟;master_process 開啟;**(程式碼=退出,狀態=203/EXEC)

這就是您可以在ExecStartPre服務單元的設置中找到的內容。您會從中註意到這PATH也是完全不相關的(這是 systemd 的設計)。該雜誌對發生的事情更加明確:

systemd [14126]:在步驟 EXEC 生成 /usr/sbin/nginx 時失敗:沒有這樣的文件或目錄

無論您怎麼想,電腦都認為在您嘗試啟動該服務時,該服務/usr/sbin/nginx無法作為程序執行,因為該名稱不存在任何文件。

我有根據的猜測是RootDirectory您的服務單元中有一個設置,並且您將文件複製到/usr/sbin不在更改的根環境中的文件,或者在更改的根環境中缺少載入程序映像所需的一些輔助共享庫。

引用自:https://unix.stackexchange.com/questions/204312