Question

I am getting a

forever: command not found error when I run a nodejs process using the forever command as a cronjob (in an amazon ec2 machine): the bash script I am using has the following code:

cd to/location/of/the/nodejs/file

forever start file.js

but I am able to run this file by using bash script.sh but I get the forever:command not found error when I run it via cronjob

I am confused dont know what could be the reason for this.

Has anybody else faced this problem?

Thanks in advance

Was it helpful?

Solution

Run this command: npm install forever -g

-g is to install it globally.

OTHER TIPS

For other users who face this problem:

you have to add the path of forever module and then run the script as a cronjob.

In linux the path normally is:

/usr/local/lib/node_modules/forever/bin/forever start file.js

just use this command in your script and the error forever:command not found will not bother you.

I know there must be some other fancy ways to do this but I am happy with this hack

in case azero0's solution doesn't work for you and you're running linux, try

sudo npm install forever -g

If you have already tried:

sudo npm install forever -g

and still get forever:command not found pay attention to the first line in the output. This should be something like:

/<node_bin_path>/forever -> /lib/node_modules/forever/bin/forever

where node_bin_path is the place in which the executable is located. This is most likely not where you expect.

This may be because node in your path is a symbolic link. In this case forever will be installed in the actual install location of node not the location of the symbolic link.

it seems forever not found in globally, use the below command to solve

npm i forever -g 
sudo npm install forever --global

this worked for me on ubuntu 16.04 server and nodejs v7.5.0

Below bash script Code Check your script running. If not running Stop and Start and notification mail.

Crontab

*/1     *       *       *       *       sh /root/yourscriptdirectory/checklive.sh >> /root/yourscriptdirectory/cron.log 2>&1

checklive.sh

chmod +x ./checklive.sh

cd /root/yourscriptdirectory/

ps xa | grep -F "/usr/local/bin/node /root/yourscriptdirectory/script.js"  | grep -Fv "grep"  > /dev/null
if [ $? -eq 0 ]; then
echo ""
else
  echo "script.js not running. Restart"
  /usr/local/bin/node /usr/local/lib/node_modules/forever/bin/forever stop script.js
  /usr/local/bin/node /usr/local/lib/node_modules/forever/bin/forever start script.js
  echo "STOP : script.js Process is stoped restarting..."  | mail -s "STOP : script.js Process is stoped restarting..." "<yourmail@gmail.com>" &
fi

You probably switched to yarn recently and installed forever with yarn, as well. Instead, fallback to npm (just for forever):

sudo npm install forever -g

( g == global )

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