Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--cron not seemingly working #496

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

--cron not seemingly working #496

stockholmux opened this issue Jun 2, 2014 · 16 comments

Comments

@stockholmux
Copy link

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
Copy link
Collaborator

soyuka commented Jul 16, 2014

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

@rlidwka
Copy link
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
Copy link
Author

@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 as completed Aug 4, 2014
soyuka added a commit that referenced this issue Aug 4, 2014
@rafaelkaufmann
Copy link

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

@naggie
Copy link

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
Copy link

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
Copy link

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
Copy link

What version of pm2? This issue is not new.

@v29neil
Copy link

v29neil commented Apr 3, 2017

pm2 --version
2.3.0

It seems to working with js file.(cluster_mode)

@vmarchaud
Copy link
Contributor

vmarchaud commented Apr 3, 2017

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

@v29neil
Copy link

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
Copy link

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
Copy link
Contributor

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
Copy link

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
Copy link

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
Copy link

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants