简体   繁体   中英

create cron to restart pm2 instance fails

I'm having troubles with restarting my instance with a cron tab doing

#!/usr/bin/node
*15 * * * * /usr/bin/node /usr/bin/pm2 restart 4

But my problem is that I have two pm2 daemons. And the one I want is in /opt.pm2 so every time I want to use pm2 I need to use this command

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?

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 . Or /usr/bin/env instead of 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. Then put this shell script as the cron command.

Doing what @sapht told me I get

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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