pm2

Using PM2 to start and stop at a particular time


Is it possible to setup pm2 to start a service at a certain time, and then kill it at a certain time. We have time apps that should only be accessible at certain times, and be turned off the rest of the time. For example, lets say I have a search API, that I only want to be up and available from 5pm to 9pm; is it possible to set this up fully in pm2?

I'm aware that you can use cron to restart in pm2; I do this with several services in the config.json:

{
    apps: [
        {
            "name" : "space_usage_monitor",
            "script" : "space_usage.py",
            "interpreter" : "python",
            "cron_restart": "30 * * * MON,TUE,WED,THU,FRI",
            "autorestart": false,
        }
    ]
}

But I don't see anything in the pm2 documentation regarding using cron for start and stops as well.


Solution

  • For doing this, use cron tasks to start and stop pm2 tasks.

    pm2 start to start the task and pm2 stop to stop the task.