Search code examples
serverchatvirtualmern

Start and stop MERN app at a particular time


Does anyone have experience of how to start and stop a MERN app cleanly in a production environment at a particular time, say start at 9 am and stop at 5pm? The application is hosted with a client-server part in a virtual environment.S TIA.


Solution

  • You can use a cron job. When the cron job is called you can kill the application or restart it.

    const cron = require('cron')
    const nrc = require('node-run-cmd')
    
    cron.schedule('* * 9 * *', () => {
      nrc.run('npm start')
    });
    
    cron.schedule('* * 17 * *', () => {
      nrc.run('pkill -f node')
    });
    
    

    If you don't want to kill all the node apps, get the pid first and use the appropriate pkill command