8

I have a .sh script in my /etc/init-d/forever to configure how forever starts and stops my Node.js app.

I wanted to start forever with the command npm start, so I can trigger my scripts from there. Is this possible?

I tried

sudo forever start --sourceDir /home/my-app -c npm start 

but its gets wrong interpreted...

info: Forever processing file: start
error: Cannot start forever
error: script /root/start does not exist.

My script so far is:

NAME=nodeapp
SOUREC_DIR=/home/nodeapp        
COMMAND="npm start"
SOURCE_NAME=index.js
USER=root
NODE_ENVIROMENT=production

pidfile=/var/run/$NAME.pid
logfile=/var/log/$NAME.log
forever=forever

start() {
    export NODE_ENV=$NODE_ENVIROMENT
    echo "Starting $NAME node instance : "

    touch $logfile
    chown $USER $logfile

    touch $pidfile
    chown $USER $pidfile

    iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
    sudo -H -u $USER $forever start --pidFile $pidfile -l $logfile -a --sourceDir $SOUREC_DIR -c $COMMAND

    RETVAL=$?
}
2
  • It's possible you need the path you your app dir after "npm start", but not sure, check: github.com/foreverjs/forever/issues/540 Mar 12, 2017 at 4:26
  • @AntonioVal I had already seen and failed with that solution. But found now how to do it! Thank you for looking onto this also!
    – Rikard
    Mar 12, 2017 at 5:39

2 Answers 2

8

So I found the answer.
Both --sourceDir and the path parameter after the "npm start"command where necessary:

sudo forever --sourceDir /home/my-app -c "npm start" /
1
  • Log won't be written in the forever log. It will directly write to the console. Jun 18, 2018 at 7:47
6

The below command run the forever command in the background with logs in forever.

forever start -c "ng serve " ./

Note the ./

Then you can

forever list

And will be able to see the status and the location of the log file.

info:    Forever processes running
data:        uid  command                                                            script forever pid   id logfile                          uptime        
data:    [0] wOj1 ng serve                                              29500   24978    /home/user/.forever/wOj1.log 0:0:25:23.326 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.