从Linux Cron重新启动PM2流程

时间:2019-06-02 00:12:00

标签: node.js linux bash cron pm2

由于某种原因,我的节点服务器每天早上2AM都通过PM2崩溃。我正在修复它,但作为权宜之计,我试图设置一个cronjob以便在站点出现故障时仅重新启动PM2进程。

我有一个如下所示的bash脚本

#!/bin/bash

status_code=$(curl --write-out %{http_code} --silent --output /dev/null SITE_URL)

if [[ "$status_code" -ne 200 ]] ; then
        pkill -f node
        echo "Site status $status_code" >> cronlog.txt
        echo "Deleting PM2 Instance" >> cronlog.txt
        /home/username/bin/node /home/username/bin/pm2 kill >> cronlog.txt
        /home/username/bin/node /home/username/bin/pm2 delete all >> cronlog.txt
        pkill -f pm2
        echo "Restarting Server" >> cronlog.txt
        /home/username/bin/node /home/username/bin/pm2 start bin/www --exp-backoff-restart-delay=100 >> cronlog.txt
        exit
else
        echo "Site Fine" >> cronlog.txt
        exit 0
fi

然后我有一个cron作业,每小时运行一次此bash脚本。但是,虽然通过SSH手动进入服务器并调用它来手动运行此脚本,但工作正常,但cron作业无效。检查我输出到的cronlog.txt文件,它肯定每小时运行一次。但是,开始步骤将如下输出

[PM2] Spawning PM2 daemon with pm2_home=/home/username/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /home/username/bin/www in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬─────────┬──────┬─────┬─────────┬─────────┬────────┬─────┬────────┬──────────┬──────────┐
│ App name │ id │ version │ mode │ pid │ status  │ restart │ uptime │ cpu │ mem    │ user     │ watching │
├──────────┼────┼─────────┼──────┼─────┼─────────┼─────────┼────────┼─────┼────────┼──────────┼──────────┤
│ www      │ 0  │ 0.0.0   │ fork │ N/A │ errored │ 0       │ 0      │ 0%  │ 0 B    │ username │ disabled │
└──────────┴────┴─────────┴──────┴─────┴─────────┴─────────┴────────┴─────┴────────┴──────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

强调errored状态,该站点仍将关闭。检查pm2.log文件显示以下内容

2019-06-01T12:00:04: PM2 log: --- New PM2 Daemon started ----------------------------------------------------
2019-06-01T12:00:04: PM2 log: Time                 : Sat Jun 01 2019 12:00:04 GMT-0400 (Eastern Daylight Time)
2019-06-01T12:00:04: PM2 log: PM2 version          : 3.5.0
2019-06-01T12:00:04: PM2 log: Node.js version      : 10.15.3
2019-06-01T12:00:04: PM2 log: Current arch         : x64
2019-06-01T12:00:04: PM2 log: PM2 home             : /home/username/.pm2
2019-06-01T12:00:04: PM2 log: PM2 PID file         : /home/username/.pm2/pm2.pid
2019-06-01T12:00:04: PM2 log: RPC socket file      : /home/username/.pm2/rpc.sock
2019-06-01T12:00:04: PM2 log: BUS socket file      : /home/username/.pm2/pub.sock
2019-06-01T12:00:04: PM2 log: Application log path : /home/username/.pm2/logs
2019-06-01T12:00:04: PM2 log: Process dump file    : /home/username/.pm2/dump.pm2
2019-06-01T12:00:04: PM2 log: Concurrent actions   : 2
2019-06-01T12:00:04: PM2 log: SIGTERM timeout      : 1600
2019-06-01T12:00:04: PM2 log: ===============================================================================
2019-06-01T12:00:05: PM2 log: App [www:0] starting in -fork mode-
2019-06-01T12:00:05: PM2 log: App [www:0] online
2019-06-01T12:00:05: PM2 error: Error: spawn node ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
2019-06-01T12:00:05: PM2 error: Cancelling versioning data parsing

由于可以通过SSH运行此bash脚本,但是cron作业没有,因此我什至尝试将cron作业SSH放入同一台计算机并以这种方式运行脚本,即ssh username@machine.address.com './fix.sh'在我的cron作业中(带有为无密码登录设置了适当的SSH证书),但仍然无法正常工作。

在这一点上,我感到非常困惑。是什么导致该脚本在手动运行时正常工作,但在通过cron运行时失败,即使cron SSH进入计算机以运行作业时也是如此? SSH进入与cron SSH进入有什么不同?

如果重要的话,服务器将运行CloudLinux 7.6。

0 个答案:

没有答案