--cron not seemingly working #496

Closed
stockholmux opened this Issue Jun 2, 2014 · 16 comments

Comments

10 participants
@stockholmux

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?

@soyuka

This comment has been minimized.

Show comment
Hide comment
@soyuka

soyuka Jul 16, 2014

Collaborator

#559
@Unitech @rlidwka should we remove the cron feature?

Collaborator

soyuka commented Jul 16, 2014

#559
@Unitech @rlidwka should we remove the cron feature?

@rlidwka

This comment has been minimized.

Show comment
Hide comment
@rlidwka

rlidwka Jul 16, 2014

Collaborator

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 * * *'
Collaborator

rlidwka commented Jul 16, 2014

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 * * *'
@stockholmux

This comment has been minimized.

Show comment
Hide comment
@stockholmux

stockholmux Jul 17, 2014

@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.

@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.

@soyuka soyuka closed this Aug 4, 2014

soyuka added a commit that referenced this issue Aug 4, 2014

@rafaelkaufmann

This comment has been minimized.

Show comment
Hide comment
@rafaelkaufmann

rafaelkaufmann Aug 22, 2014

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

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

@naggie

This comment has been minimized.

Show comment
Hide comment
@naggie

naggie Mar 26, 2015

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!

naggie commented Mar 26, 2015

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!

@michaelBenin

This comment has been minimized.

Show comment
Hide comment
@michaelBenin

michaelBenin Jun 4, 2016

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.

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.

@v29neil

This comment has been minimized.

Show comment
Hide comment
@v29neil

v29neil Apr 3, 2017

@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.

v29neil commented Apr 3, 2017

@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.

@michaelBenin

This comment has been minimized.

Show comment
Hide comment
@michaelBenin

michaelBenin Apr 3, 2017

What version of pm2? This issue is not new.

What version of pm2? This issue is not new.

@v29neil

This comment has been minimized.

Show comment
Hide comment
@v29neil

v29neil Apr 3, 2017

pm2 --version
2.3.0

It seems to working with js file.(cluster_mode)

v29neil commented Apr 3, 2017

pm2 --version
2.3.0

It seems to working with js file.(cluster_mode)

@vmarchaud

This comment has been minimized.

Show comment
Hide comment
@vmarchaud

vmarchaud Apr 3, 2017

Collaborator

Currently cron_restart is only working inside JS process, see #2487

Collaborator

vmarchaud commented Apr 3, 2017

Currently cron_restart is only working inside JS process, see #2487

@v29neil

This comment has been minimized.

Show comment
Hide comment
@v29neil

v29neil Apr 3, 2017

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

v29neil commented Apr 3, 2017

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

@v29neil

This comment has been minimized.

Show comment
Hide comment
@v29neil

v29neil Apr 3, 2017

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

v29neil commented Apr 3, 2017

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

@vmarchaud

This comment has been minimized.

Show comment
Hide comment
@vmarchaud

vmarchaud Apr 3, 2017

Collaborator

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)

Collaborator

vmarchaud commented Apr 3, 2017

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)

@shtse8

This comment has been minimized.

Show comment
Hide comment
@shtse8

shtse8 May 13, 2017

@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.

shtse8 commented May 13, 2017

@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.

@leodutra

This comment has been minimized.

Show comment
Hide comment
@leodutra

leodutra Apr 26, 2018

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.

leodutra commented Apr 26, 2018

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.

@leodutra

This comment has been minimized.

Show comment
Hide comment
@leodutra

leodutra Apr 26, 2018

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

leodutra commented Apr 26, 2018

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment