7

I am using PM2 to keep my node.js apps running.

Is there any way to have PM2 restart my app every 1 hour?

Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
faressoft
  • 19,053
  • 44
  • 104
  • 146

4 Answers4

9

Put the code below in pm2.js and start it with pm2 start pm2.js

    var pm2 = require('pm2');

  pm2.connect(function(err) {
    if (err) throw err;

  setTimeout(function worker() {
    console.log("Restarting app...");
    pm2.restart('app', function() {});
    setTimeout(worker, 1000);
    }, 1000);
  });

More about this can be found here.

Additional resources:

Community
  • 1
  • 1
Tomislav Stankovic
  • 3,080
  • 17
  • 35
  • 42
4

Use crontab.
Append this under your crontab file (run with crontab -e):

0 * * * * pm2 restart yourappname

Note that if you don't want to increment your pm2 restart counter, you can do something like this:

0 * * * * pm2 stop yourappname && pm2 start yourappname

Explanation:

0: on the 0th minute of the hour
*: every hour
*: every day
*: every month
*: every day of the week

Binary
  • 451
  • 5
  • 15
  • Do you have any reference link for this? – Shubham Dixit May 12 '20 at 21:16
  • [CronHowto](https://help.ubuntu.com/community/CronHowto) "An asterisk (*) can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used." – Binary May 12 '20 at 21:20
4

You can now use the pm2 built-in --cron-restart option, documented here. In your case, to restart every hour, you could add this to your pm2 config file.

    cron_restart: '0 * * * *',
now
  • 4,772
  • 2
  • 24
  • 26
  • This is by far the best answer. Everything you need is all part of pm2 and you don't have to include and maintain any cron scripts. – d512 May 17 '23 at 16:25
0

Here is how I did it. Then: pm2 start name_of_your_file.js.

In my case, it will exit the script after 15 minutes.

/**************************************************************************
 * IMPORTS
 ***************************************************************************/

// NPM
const consola = require('consola')
const pm2 = require('pm2')

/**************************************************************************
 * START PM2 PROGRAMATICALLY
 ***************************************************************************/

pm2.connect((error) => {
  if (error) {
    console.error(error)
    process.exit(2)
  }

  pm2.start({ script: 'brain/domains.js' }, (error, apps) => {
    pm2.disconnect() // Disconnects from PM2

    if (error) {
      console.error(error)
      process.exit(2)
    }
  })

  // Kill the process if taking longer than expected
  setInterval(() => {
    pm2.describe('domains', (error, scripts) => {
      const exitTimeout = 15
      const uptime = Date.now() - scripts[0].pm2_env.pm_uptime

      if (uptime > exitTimeout * 60 * 1000) {
        consola.info(`Closing cluster after ${exitTimeout} minutes...`)

        pm2.restart('domains', (error) => {
          if (error) {
            console.error(error)
            process.exit(2)
          }
        })
      }
    })
  }, 30 * 1000)
})

Julien Le Coupanec
  • 7,742
  • 9
  • 53
  • 67