简体   繁体   English

如何使用cron重启PM2?

[英]How to restart PM2 using cron?

I need to find a cron command that would restart pm2 with a process but only if it's not already running我需要找到一个 cron 命令,该命令可以使用进程重新启动 pm2,但前提是它尚未运行

pm2 start app.js starts the app even if it's already running. pm2 start app.js启动应用程序,即使它已经在运行。

what would be another command I could use that would only restart app.js if it's not already running and how would I write it in crontab?如果app.js尚未运行,我可以使用的另一个命令是什么,它只会重新启动app.js ,我将如何在 crontab 中编写它?

There is no default way in pm2 to achieve this instead of that you can write a shell script for that.在 pm2 中没有默认方法来实现这一点,而不是您可以为此编写一个 shell 脚本。

#!/bin/bash

pm2 describe appname > /dev/null 
RUNNING=$?  

if [ "${RUNNING}" -ne 0 ]; then
    pm2 start ./yourscriptpath
else
    pm2 restart appname
fi;

Save this shell script as pm2_starter.sh and then将此 shell 脚本另存为pm2_starter.sh ,然后

crontab -e

and add following there并在那里添加以下内容

 */30 * * * * ./home/ridham/stackoverflow/pm2_starter.sh

this will run it every 30 minutes.这将每 30 分钟运行一次。

Here the script will restart if app is running under pm2 and else it will start it.如果应用程序在 pm2 下运行,脚本将重新启动,否则它将启动它。 You are smart enough to edit that as per your use case您足够聪明,可以根据您的用例进行编辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM