Search code examples
shellcronpm2

Shell node: No such file or directory


I have a cron job that runs a shell script every minute. However I keep getting

/usr/bin/env: node: No such file or directory
restarting
nohup: failed to run command ‘npm’: No such file or directory

as an output.

I have tried installing pm2 globally but this doesnt work.

This is my Shell file:

#!/bin/bash
PATH=$PATH: /home/dev/bin/npm
pID=$(pgrep -x "PM2") 

if [ -n "${pID}" ];
then
    #do nothing 
    echo $pID "already running. not restarting." 
else
    # start it 
    echo "restarting"
    nohup npm ./home/dev/public_node/server.js --production &
fi

It should start the server.js file through pm2?


Solution

  • I would like to thank @Florian Schlag for helping me reach the answer and giving me the correct answer. I have pasted my file below for reference. Please note that I had to change some files around but these can be deduced from output npm errors.

    #!/bin/bash
    PATH=$PATH:/home/<user>/bin/
    NPM="`which npm`"
    
    if [ "x" == "x$NPM" ]; then
        (>&2 echo "NPM not installed")
        exit 1
    fi
    
    pID=$(pgrep "PM2" -f) 
    
    if  [ -n "${pID}" ];
    then
        exit 0
    else
        # start it 
        echo "restarting"
        nohup $NPM start ./<file path ton script> --production &
    fi