Monday, 15 May 2017

Bash script to trap SIGINT and SIGTERM fails to start node js server

I've written a bash script to trap SIGINT and SIGTERM events, but now the command to start the nodejs server fails, here's the script:

#!/usr/bin/env bash

# setup process id variable
pid=0

handle_signal (){
  echo "received signal $2"
  case "$2" in
    SIGINT)
      kill -SIGTERM "$pid"
      wait "$pid"
      ;;
    SIGTERM)
      kill -SIGINT "$pid"
      wait "$pid"
      ;;
  esac
}

trap 'kill ${!}; handle_signal' SIGINT
trap 'kill ${!}; handle_signal' SIGTERM

# run application
node program &
pid="$!"

# wait forever
while true
do
  tail -f /dev/null & wait ${!}
done



via David Faiz

No comments:

Post a Comment