Why can't node.js grab SIGTERM from an upstart script?

When I stop the upstart, SIGTERM is used by default. I grabbed SIGTERM in node.js as:

process.on('SIGTERM', shutdownServerGracefully);
process.on('SIGINT', shutdownServerGracefully);

I checked that this works by running it from the terminal and sending kill -s SIGTERM [pid]. This causes a graceful shutdown, and I see it in the logs.

I have an upstart script that looks like this:

# Process will start after a network is available.
start on runlevel [2345]
stop on runlevel [016]

# Keep server running
respawn
respawn limit 5 30

# Wait 5 minutes for server to gracefully shutdown
kill timeout 300
# Allow for graceful shutdown
kill signal SIGTERM
normal exit 0 SIGTERM SIGINT SIGQUIT
chdir /var/web_server

script
  # Environment variables
  . /etc/environment
  export NODE_ENV

  # Setup all stdout and stderr to go to a named pipe
  mkfifo /tmp/log-fifo
  ( logger -t web < /tmp/log-fifo & )
  exec > /tmp/log-fifo
  rm /tmp/log-fifo

  node server.js --env="$NODE_ENV" 2>&1
end script

When I start work, everything works fine. When I stop working, the node process is immediately killed, node never processes the SIGTERM signal.

If I change upstart conf to kill signal SIGINT, the node process will get SIGINT and the shutdown will be elegant. WTF?

SIGINT , SIGTERM? node SIGTERM , Upstart, SIGINT? Upstart?

+4

Source: https://habr.com/ru/post/1652708/


All Articles