Question

I'm trying to execute Forever.js on System Restarts using a bash script (named starter.sh) to check if my app is running or not:

#!/bin/sh
if [ $(ps -e -o uid,cmd | grep $UID | grep node | grep -v grep | wc -l | tr -s "\n") -eq 0 ]
then
        export PATH=/usr/local/bin:$PATH
        forever start --sourceDir ~/var/www/mysite app.js >> ~/var/www/mysite/log.txt 2>&1
fi

Then I've appended the following code to crontab:

@reboot ~/var/www/mysite/starter.sh

but after restarting the system (sudo reboot) Forever.js doesn't start. In the log file I receive the following messages:

/root/var/www/mysite/starter.sh: 6: 
/root/var/www/mysite/starter.sh: forever: not found

Any idea?

P.S. if I call Forever from command line (forever start --sourceDir ~/var/www/mysite app.js) all work properly.

Was it helpful?

Solution 2

the missing piece:

n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local

This series of commands put forever into /usr/local/bin/.

OTHER TIPS

I would look into something like upstart to start/stop your node scripts on reboot. This post goes into a lot of detail about doing exactly what you're after, and you can possible simplify the setup a bit for your needs:

https://www.exratione.com/2013/02/nodejs-and-forever-as-a-service-simple-upstart-and-init-scripts-for-ubuntu/

But if you're not running ubuntu or similar, each environment has its own start/stop services type of thing. On Mac OS X you can use launchd instead. launchd has a lot of features, but hopefully this post can guide you in the right direction:

http://paul.annesley.cc/2012/09/mac-os-x-launchd-is-cool/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top