node.jsbashcronforever

Start forever using bash if it is not executing


I have a socket application, which is created in node js. I was using forever to run this application. Due to some reason, forever is stopping automatically. I have created a script to check the port is available or not and if the port is free, then start forever using the following script. using bash it is starting, but when we close the terminal, forever is also terminating.

if lsof -Pi :3000 -sTCP:LISTEN -t >/dev/null ; then
    echo "running"
else
    forever  /home/ubuntu/socket_app_modular/server.js start
fi

How to use this script to start forever, if it is not running?


Solution

  • You should put forever process in background:

    1. You can use & sign to do it forever /home/ubuntu/socket_app_modular/server.js start &
    2. You can use nohup command
    3. Or any other way to do it