[英]create cron to restart pm2 instance fails
I'm having troubles with restarting my instance with a cron tab doing 我在使用cron选项卡重新启动实例时遇到麻烦
#!/usr/bin/node
*15 * * * * /usr/bin/node /usr/bin/pm2 restart 4
But my problem is that I have two pm2 daemons. 但是我的问题是我有两个pm2守护程序。 And the one I want is in /opt.pm2 so every time I want to use pm2 I need to use this command 我想要的那个在/opt.pm2中,所以每次我想使用pm2时,我都需要使用此命令
export PM2_HOME="/opt/.pm2"
and I need to know how to pass this env variable inside my crontab to restart instance 4 cause in /usr/bin/pm2 I don't have my app running understand? 而且我需要知道如何在crontab中传递此env变量以重新启动实例4,原因是在/ usr / bin / pm2中我没有运行我的应用程序?
But I get the error 但是我得到了错误
From root@ip120.ip-46-105-248.eu Sun Nov 12 11:23:01 2017
Return-Path: <root@ip120.ip-46-105-248.eu>
X-Original-To: root
Delivered-To: root@ip120.ip-46-105-248.eu
Received: by ip120.ip-46-105-248.eu (Postfix, from userid 0)
id E84CA100921; Sun, 12 Nov 2017 11:23:01 +0000 (WET)
From: "(Cron Daemon)" <root@ip120.ip-46-105-248.eu>
To: root@ip120.ip-46-105-248.eu
Subject: Cron <root@ip120> /usr/bin/node /usr/bin/pm2 restart 4
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=4300>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20171112112301.E84CA100921@ip120.ip-46-105-248.eu>
Date: Sun, 12 Nov 2017 11:23:01 +0000 (WET)
Use --update-env to update environment variables
[PM2] Applying action restartProcessId on app [4](ids: 4)
[PM2][ERROR] Process 4 not found
┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
└──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────────┘
Module activated
┌───────────────┬─────────┬────────────┬────────┬─────────┬─────┬─────────────┐
│ Module │ version │ target PID │ status │ restart │ cpu │ memory │
├───────────────┼─────────┼────────────┼────────┼─────────┼─────┼─────────────┤
│ pm2-logrotate │ 2.2.0 │ N/A │ online │ 0 │ 0% │ 91.223 MB │
└───────────────┴─────────┴────────────┴────────┴─────────┴─────┴─────────────┘
Use `pm2 show <id|name>` to get more details about an app
If all other things are equal except the environment variable you can set your crontab command to env PM2_HOME="/opt/.pm2" /usr/bin/node /usr/bin/pm2 restart 4
. 如果除环境变量外所有其他条件都相同,则可以将crontab命令设置为env PM2_HOME="/opt/.pm2" /usr/bin/node /usr/bin/pm2 restart 4
。 Or /usr/bin/env
instead of env
. 或/usr/bin/env
而不是env
。
If you don't find success this way, you can write a small shell script that just exports the PM2_HOME value and restarts pm2. 如果您无法通过这种方式获得成功,则可以编写一个小的Shell脚本,该脚本仅导出PM2_HOME值并重新启动pm2。 Then put this shell script as the cron command. 然后将这个shell脚本作为cron命令。
Doing what @sapht told me I get 做@sapht告诉我的事情
crontab -l
#!/usr/bin/node
*/45 * * * * env PM2_HOME="/opt/.pm2" /usr/bin/node /usr/bin/pm2 restart 4
I get this output 我得到这个输出
From root@ip120.ip-46-105-248.eu Sun Nov 12 12:15:02 2017
Return-Path: <root@ip120.ip-46-105-248.eu>
X-Original-To: root
Delivered-To: root@ip120.ip-46-105-248.eu
Received: by ip120.ip-46-105-248.eu (Postfix, from userid 0)
id 7FC6010092C; Sun, 12 Nov 2017 12:15:02 +0000 (WET)
From: "(Cron Daemon)" <root@ip120.ip-46-105-248.eu>
To: root@ip120.ip-46-105-248.eu
Subject: Cron <root@ip120> env PM2_HOME="/opt/.pm2" /usr/bin/node /usr/bin/pm2 restart 4
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=4304>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20171112121502.7FC6010092C@ip120.ip-46-105-248.eu>
Date: Sun, 12 Nov 2017 12:15:02 +0000 (WET)
Use --update-env to update environment variables
[PM2] Applying action restartProcessId on app [4](ids: 4)
[PM2] [wscontroller-gps-service](4) ✓
┌──────────────────────────┬────┬─────────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
├──────────────────────────┼────┼─────────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ wscontroller-api │ 2 │ cluster │ 25521 │ online │ 0 │ 47m │ 0% │ 47.6 MB │ disabled │
│ wscontroller-gps-service │ 4 │ fork │ 2974 │ online │ 5 │ 0s │ 99% │ 14.0 MB │ disabled │
│ wscontroller-proxy │ 5 │ cluster │ 25549 │ online │ 0 │ 47m │ 0% │ 45.5 MB │ disabled │
│ wscontroller-service │ 3 │ cluster │ 29681 │ online │ 1 │ 28m │ 31% │ 83.6 MB │ disabled │
│ wscontroller-ui-server │ 1 │ cluster │ 25515 │ online │ 0 │ 47m │ 0% │ 35.7 MB │ disabled │
└──────────────────────────┴────┴─────────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘
Module activated
┌───────────────┬─────────┬────────────┬────────┬─────────┬─────┬─────────────┐
│ Module │ version │ target PID │ status │ restart │ cpu │ memory │
├───────────────┼─────────┼────────────┼────────┼─────────┼─────┼─────────────┤
│ pm2-logrotate │ 2.2.0 │ N/A │ online │ 0 │ 0% │ 36.527 MB │
└───────────────┴─────────┴────────────┴────────┴─────────┴─────┴─────────────┘
Use `pm2 show <id|name>` to get more details about an app
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.