问题描述
我想通过
pm2 restart app.js
使用crontab,但仅在应用尚未运行时使用(例如,如果我的服务器崩溃并重新启动,而pm2没有重新启动).
using crontab but ONLY if the app is not already running (e.g. if my server crashed and restarted and pm2 didn't restart).
以上命令无论如何都将重新启动它.
the command above restarts it anyway even if it's running.
我该如何解决?
更新
如果我的应用程序已经在运行,我不希望它重新启动.如果它被列为已停止"或未运行,我希望它仅重新启动 .一些建议可以编写bash脚本,但是会是什么呢?我尝试了以下选项,但它们要么无法运行,要么即使正在运行,也无法重新启动该应用程序.
I do not want my app to restart if it's already running. I want it to restart only if it's listed as "stopped" or if it is not running.Some suggestions offer to write a bash script, but what would it be? I tried the options below, but they either don't work or restart the app even if it's running.
推荐答案
更好的方法是使用pm2 startup
命令
http://pm2.keymetrics.io/docs/usage/startup/
要获取计算机的自动配置的启动脚本,您需要输入以下命令:
To get the automatically-configured startup script for your machine you need to type this command:
# Detect available init system, generate configuration and enable startup system
pm2 startup
您可以根据需要指定自己使用的平台(平台可以是上面引用的任一平台):
You can specify the platform you use by yourself if you want to (where platform can be either one of the cited above):
pm2 startup [ubuntu | ubuntu14 | ubuntu12 | centos | centos6 | arch | oracle | amazon | macos | darwin | freebsd | systemd | systemv | upstart | launchd | rcd | openrc]
此命令的输出可以作为要复制/粘贴的行的建议,并为您配置了所有环境变量和选项.
The output of this command can be a recommendation of the line to copy/paste with all environment variables and options configured for you.
示例:
您只需要复制/粘贴PM2行,即可为您的操作系统配置启动脚本.
You simply have to copy/paste the line PM2 gives you and the startup script will be configured for your OS.
一旦运行sudo pm2 startup
.它将创建systemctl service
.您可以使用
Once you run the sudo pm2 startup
. It will create the systemctl service
. You can check the status of the same using
systemctl status pm2-root
默认情况下,服务未配置为自动重新启动.您将运行以下命令
By default the service is not configure to restart automatically. You will run the below commands
sudo mkdir -p /etc/systemd/system/pm2-root.service.d
,然后使用以下内容创建文件名10_auto_restart_pm2.conf
and then create a file name 10_auto_restart_pm2.conf
with below content
[Service]
Restart=always
RestartSec=3
之后执行
systemctl daemon-reload
systemctl restart pm2-service
现在让我们测试一下自动重启部分
Now let's test the auto restart part
$ systemctl status pm2-root.service
● pm2-root.service - PM2 process manager
Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/pm2-root.service.d
└─10_auto_restart_pm2.conf
Active: active (running) since Wed 2018-02-28 16:52:19 UTC; 11s ago
Docs: https://pm2.keymetrics.io/
Process: 5014 ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill (code=exited, status=0/SUCCESS)
Process: 5022 ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=0/SUCCESS)
Main PID: 5031 (PM2 v2.10.1: Go)
Tasks: 9
Memory: 24.3M
CPU: 460ms
CGroup: /system.slice/pm2-root.service
└─5031 PM2 v2.10.1: God Daemon (/home/vagrant/.pm2)
现在我们手动终止该过程并等待3秒
Now we kill the process manually and wait for 3 seconds
$ kill -9 5031
$ sleep 3
$ systemctl status pm2-root.service
● pm2-root.service - PM2 process manager
Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/pm2-root.service.d
└─10_auto_restart_pm2.conf
Active: active (running) since Wed 2018-02-28 16:52:55 UTC; 641ms ago
Docs: https://pm2.keymetrics.io/
Process: 5057 ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill (code=exited, status=0/SUCCESS)
Process: 5081 ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=0/SUCCESS)
Main PID: 5088 (PM2 v2.10.1: Go)
Tasks: 9
Memory: 24.3M
CPU: 461ms
CGroup: /system.slice/pm2-root.service
└─5088 PM2 v2.10.1: God Daemon (/home/vagrant/.pm2)
您可以看到进程/服务已自动重新启动.不需要cron,这就是您应该怎么做.
As you can see the process/service was restarted automatically. No cron needed and it is how you should do it.
这篇关于有没有办法使用cron重新启动pm2进程,但前提是它尚未运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!