Shell节点:没有那个文件或目录

Shell node: No such file or directory

我有一个 cron 作业,每分钟运行一个 shell 脚本。但是我不断收到

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

作为输出。

我试过全局安装 pm2,但这不起作用。

这是我的 Shell 文件:

#!/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

应该通过pm2启动server.js文件?

您将之前的问题从未找到 pm2 编辑为未找到 node,但是解决此问题的一种方法 是确定像这样的任意工具:

NPM="`which npm`"

if [ "x" == "x$NPM" ]; then
    (>&2 echo "NPM not installed")
    exit 1
fi

# Run by using the variable like it's a regular command
# e.g.
nohup $NPM ./home/dev/public_node/server.js --production &

感谢@Florian Schlag 帮我找到答案并给我正确答案。我在下面粘贴了我的文件以供参考。请注意,我不得不更改一些文件,但这些可以从输出 npm 错误中推断出来。

#!/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