Pm2: --cron not seemingly working

Created on 2 Jun 2014  ยท  16Comments  ยท  Source: Unitech/pm2

I can't seem to get pm2's cron to actually restart a process - take this example:

kyle@--------------:~/nodeSites/gustavus$ date
Mon Jun  2 08:34:58 EDT 2014
kyle@--------------:~/nodeSites/gustavus$ pm2 start wpServices.node.js -n wpService --cron 36 8 * * * -x
36
PM2 Process launched
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ App Name    โ”‚ id โ”‚ mode    โ”‚ PID   โ”‚ status โ”‚ port โ”‚ Restarted โ”‚ Uptime โ”‚    memory โ”‚ err logs                                   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ wpService   โ”‚ 17 โ”‚ fork    โ”‚ 3228  โ”‚ online โ”‚      โ”‚         0 โ”‚ 0s     โ”‚  4.719 MB โ”‚ /home/kyle/.pm2/logs/wpService-err-17.log  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
kyle@--------------:~/nodeSites/gustavus$ date
Mon Jun  2 08:35:17 EDT 2014
kyle@--------------:~/nodeSites/gustavus$ date
Mon Jun  2 08:36:09 EDT 2014
kyle@--------------:~/nodeSites/gustavus$ pm2 list
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ App Name    โ”‚ id โ”‚ mode    โ”‚ PID   โ”‚ status โ”‚ port โ”‚ Restarted โ”‚ Uptime โ”‚    memory โ”‚ err logs                                   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ wpService   โ”‚ 17 โ”‚ fork    โ”‚ 3228  โ”‚ online โ”‚      โ”‚         0 โ”‚ 69s    โ”‚ 25.773 MB โ”‚ /home/kyle/.pm2/logs/wpService-err-17.log  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

(other process were redacted)

Am I doing something wrong or is this an actual bug?

Most helpful comment

@rlidwka thanks for the update. I'll try that.

Just to chime in on the need of this - it's nice to be able to do everything with PM2 for deployment instead of having to setup the pm2 start then edit the crontab file. It took a minute to figure out the directories for using crontab. It's not really that straight forward:

58 10 * * * /home/user-name/local/bin/node /home/user-name/local/lib/node_modules/pm2/bin/pm2 restart wpService

If I had a vote, I'd keep it, maybe just document a bit better.

All 16 comments

559

@Unitech @rlidwka should we remove the cron feature?

Yes, I don't see why someone would need it if he has an actual cron (other than for b/w compatibility).

That said, @stockholmux 's error is just about escaping strings in bash command line. This should work better (didn't test though):

$ pm2 start wpServices.node.js -n wpService --cron '36 8 * * *'

@rlidwka thanks for the update. I'll try that.

Just to chime in on the need of this - it's nice to be able to do everything with PM2 for deployment instead of having to setup the pm2 start then edit the crontab file. It took a minute to figure out the directories for using crontab. It's not really that straight forward:

58 10 * * * /home/user-name/local/bin/node /home/user-name/local/lib/node_modules/pm2/bin/pm2 restart wpService

If I had a vote, I'd keep it, maybe just document a bit better.

I'm with @stockholmux on this one. I love having cron_restart on the JSON config file.

Yes cron-like timing is very useful. I currently have 10 processes that I run for local development at work, together with a few maintenance tasks. Having everything in one JSON file is great!

Hey, just to add to this, I've noticed that having a config where:

{
  "apps": [
    {
      "name": "cron-script",
      "cwd": ".",
      "script": "dist/server/index.js",
      "exec_mode": "fork_mode",
      "min_uptime": 1000,
      "watch": false,
      "merge_logs": true,
      "log_file": "logs/pm2_child.log",
      "error_file": "logs/pm2_error.log",
      "out_file": "logs/pm2_child_out.log",
      "exec_interpreter": "node",
      "env": {
        "NODE_ENV": "production"
      },
      "pmx": false,
      "vizion": false,
      "cron_restart": "*/1 * * * *", // restart every minute (used for testing)
      "autorestart": false // this is the problem here
    }
  ]
}

Having autorestart false disables the cron restart. I want to be able to exit the process gracefully after the script runs. My work around is to close all the db connections and sockets after the script runs but do not exit it. I feel like this is an issue with the cron_restart.

@michaelBenin any update on this??
i simply created a bash file

#!/usr/bin/env bash
echo "test"
exit 0

My config file is this :

 apps:[{
  "name": "test",
  "script": "./backup/test.sh",
  "exec_interpreter": "bash",
  "exec_mode"  : "fork_mode",
  "cron_restart": "* * * * *",
  "autorestart": true
}]

but cron_restart doesnt work.

What version of pm2? This issue is not new.

pm2 --version
2.3.0

It seems to working with js file.(cluster_mode)

Currently cron_restart is only working inside JS process, see https://github.com/Unitech/pm2/issues/2487

but according to https://github.com/Unitech/pm2/blob/master/CHANGELOG.md#0147
It should work. correct me if i am wrong.

I changed my script

echo "test"
keepgoing=1
trap '{ echo "sigint"; keepgoing=0; }' SIGINT

while (( keepgoing )); do
    echo "sleeping"
    sleep 5
done

So that it should end gracefully , just for testing purpose. But no luck. Its not working

Actually no, the change was that its the process container that ask to pm2 to be killed (see https://github.com/Unitech/pm2/blob/master/lib/ProcessContainerFork.js#L20)

@michaelBenin Any updates? I have the same problem with --no-autorestart. If I don't have this flag, my program will be auto restarted 15 times and then got error. But if I enable this flag, my cron will not restart the program.

Any updates?

Is there a PM2 way to run cron_restart with autorestart: false?
If yes, is exec_mode: 'cluster' required or am I able to run it with fork?

Is PM2 cron_restart intended to "ease" deployment configs and "replace" crontab for PM2 apps?

We need more docs on it.

I built this file and am sharing just to help other devs.
It follows the workaround of @stockholmux

This will consider you listed your routines as apps with autorestart: false inside your ecosystem.
The reason for this is use the same set of env_* definitions for the microservice and its routines.

Run the microservice using the --only parameter, probably in your post-deploy hook or process file, and remind to configure routines as autorestart: false.

The builder will automatically consider PM2 path from NVM default alias and your project root path.

Just need to edit * * * * *, "PM2_ENVS" AND "ROUTINES_NAMES" for your expectations.

#!/bin/sh
# RUN THIS FILE INSIDE YOUR PROJECT ROOT ON THE FINAL ENVIRONMENT HOST

PM2_ENVS=('development' 'staging' 'production')
ROUTINES_NAMES=('pm2-app-a', 'pm2-app-b')

SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
PROJ_DIR="$SCRIPTPATH"
NODE_VERSION=$(cat ~/.nvm/alias/default)
NODE_NVM="~/.nvm/versions/node/v$NODE_VERSION"
NODE="$NODE_NVM/bin/node"
PM2="$NODE_NVM/lib/node_modules/pm2/bin/pm2"

for PM2_ENV in "${PM2_ENVS[@]}"
do
    echo "# PM2 ENVIRONMENT=$PM2_ENV"
    for ROUTINE_NAME in "${ROUTINES_NAMES[@]}"
    do
        echo "* * * * * $NODE $PM2 restart '$PROJ_DIR/ecosystem.config.js' --env '$PM2_ENV' --only '$ROUTINE_NAME'"
    done
done
Was this page helpful?
0 / 5 - 0 ratings

Related issues

webchaz picture webchaz  ยท  3Comments

alexpts picture alexpts  ยท  3Comments

cchan picture cchan  ยท  3Comments

chaos-git picture chaos-git  ยท  3Comments

morfies picture morfies  ยท  3Comments