43

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

azero0
  • 2,220
  • 3
  • 20
  • 31

8 Answers8

59

Run this command: npm install forever -g

-g is to install it globally.

dcoles
  • 3,785
  • 2
  • 28
  • 26
sunitj
  • 1,215
  • 1
  • 12
  • 21
10

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

azero0
  • 2,220
  • 3
  • 20
  • 31
  • 5
    Running `sudo ln -s /usr/local/lib/node_modules/forever/bin/forever /usr/bin/forever` would create a simlink to allow you to run `sudo forever ...` – Sators Jan 13 '17 at 13:13
10

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

sudo npm install forever -g
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
babycakes
  • 547
  • 7
  • 21
4

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.

meesern
  • 2,518
  • 27
  • 29
  • I think I have a problem related to this. So what is the solution? One day I ran `sudo npm config set prefix '~/.npm-global'` and I think this is causing the problem. – Vahid Feb 08 '19 at 21:13
  • Found the solution! Reset the configurations with this link: https://stackoverflow.com/a/20934521/1889607 – Vahid Feb 08 '19 at 21:51
1

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

npm i forever -g 
KARTHIKEYAN.A
  • 18,210
  • 6
  • 124
  • 133
0
sudo npm install forever --global

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

ali ozkara
  • 5,425
  • 2
  • 27
  • 24
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
Yasin Bikmazer
  • 104
  • 1
  • 8
0

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 )

dylanh724
  • 948
  • 1
  • 12
  • 29