title: Start/stop cronjob on button click in Nodejs Express app
body: <p>I have been working on a project which requires the start and stop of cron scheduler when a user clicks on a button on the front end. Basically when a user clicks on a button, the cron job will start. And clicking the stop button will stop the timer. It is as simple as that.</p>
  <p>To achieve that, I am making post requests to the Nodejs/Express backend on button click which triggers start/stop function of the scheduler. This is how the endpoint looks like:</p>
  <pre><code>const cron = require('node-cron');
  
  router.post('/scheduler', async (req, res) =&gt; {
  
      // gets the id from the button
      const id = req.body.id;
  
      try{
           // finds the scheduler data from the MongoDB
           const scheduler = await Scheduler.find({ _id: id });
  
           // checks whether there is a scheduler or not
           if ( !scheduler  ) {
               return res.json({
                    error: 'No scheduler found.'
               });
           }
  
           // creates the cronjob instance with startScheduler 
           const task = cron.schedule('*/10 * * * * *', () =&gt;  {
                console.log('test cronjob running every 10secs');
           }, {
                scheduled: false
           });
  
           // checks if the scheduler is already running or not. If it is then it stops the scheduler
           if ( scheduler.isRunning ) {
  
               // scheduler stopped
               task.stop();
  
               return res.json({
                    message: 'Scheduler stopped!'
               });
           }
  
           // starts the scheduler
           task.start();
  
           res.json({
                message: 'Scheduler started!'
           });
  
      }catch(e) {
           console.log(e)
      }
  });
  </code></pre>
  <p>Right now the scheduler runs perfectly but it doesn't stop on second button click. It keeps on running. I feel like I'm not calling <code>task.start()</code> and <code>task.stop()</code> at correct places where it would work. And I don't know where the correct places are. I'm actually new to cronjobs.</p>
  <p>It would be great if someone tells me what I am doing wrong.</p>
  <p>Thanks in advance.</p>