Nginx init.d script

After a dist-upgrade on my debian machine yesterday I am having some problems with the nginx init script. Whenever I try to start nginx using the init script it comes up with a error message:

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

Output of 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)

Output of 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.

my nginx file in init.d:

#! /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

The nginx daemon file is located in /usr/local/sbin/nginx (I compiled it there).

Things i’ve tried so far:
I copied the daemon file /usr/local/sbin/nginx to /usr/sbin/nginx

When I use service nginx start it hangs on starting nginx. When I try to connect to port 80 it does work. So I proceeded to edit my init script and change PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin to PATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/sbin:/usr/bin. This however gave the same result.

I haven’t triedd anything else so far.

Asked By: Sander Koenders

||

my nginx file in init.d:

… is entirely irrelevant. Look at the output of systemctl. It told you exactly what file to look at:

Loaded: loaded (/lib/systemd/system/nginx.service; enabled)

That is your service unit. And systemctl is telling you what that service unit has configured for preparing to start the service:

 Process: 14126 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=203/EXEC)

That’s what you’ll find in the ExecStartPre setting in the service unit. You’ll notice from it that PATH is also entirely irrelevant (which is by design in systemd). The journal was even more explicit about what happened:

systemd[14126]: Failed at step EXEC spawning /usr/sbin/nginx: No such file or directory

Whatever you may think, the computer thinks that at the time that you tried to start that service /usr/sbin/nginx could not be run as a program because no file existed by that name.

My educated guess is that you have a RootDirectory setting in your service unit, and either you copied the file to the /usr/sbin that is not within the changed root environment or some ancillary shared libraries necessary for loading the program image are missing from the changed root environment.

Answered By: JdeBP

I had same issue in debian 8. For me I find

[date] debian systemd[1]: nginx.service never wrote its PID file. Failing.

at /var/log/syslog, which is similar as the question

# journalctl -xn
systemd[1]: nginx.service never wrote its PID file. Failing.

The reason is that in /lib/systemd/system/nginx.service I have

PIDFile=/var/run/nginx.pid

But since the nginx is build it writes pid at /usr/local/nginx/log/nginx.pid

The fix is to change/add the line at /usr/local/nginx/conf/nginx.conf

pid        /var/run/nginx.pid;
Answered By: joe
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.