Use "npm start" as command to forever in .sh script

13,049

Solution 1

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" /

Solution 2

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 
Share:
13,049
Rikard
Author by

Rikard

Updated on June 07, 2022

Comments

  • Rikard
    Rikard almost 2 years

    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=$?
    }
    
  • Krishnadas PC
    Krishnadas PC almost 6 years
    Log won't be written in the forever log. It will directly write to the console.