title: Sending emails when Node.js app crashes with forever or forever-monitor
body: <p>I'm using <code>forever</code> to run my Node.js server and I very like it's <code>start</code>/<code>stop</code>/<code>restart</code> command line commands. Recently I decided that I need system that will email me about all server crashes. Unfortunately <code>forever</code> doesn't support such kind of functionality out of the box.</p>
  
  <p>So I decided to take <code>forever-monitor</code> (the tool like <code>forever</code> but with programmatic approach) and <code>nodemailer</code>. Here is a script I wrote:</p>
  
  <pre><code>var forever = require('forever-monitor');
  var nodemailer = require('nodemailer');
  var sendmailTransport = require('nodemailer-sendmail-transport');
  
  var transporter = nodemailer.createTransport(sendmailTransport({
    path: '/usr/sbin/sendmail'
  }));
  
  var error = '';
  
  function sendMail(subject, text) {
    var message = {
      // sender info
      from: '"Server name" &lt;no-reply@example.com&gt;',
      // Comma separated list of recipients
      to: '"My name" &lt;myemail@example.com&gt;',
      // Subject of the message
      subject: subject,
      // plaintext body
      text: text
    };
  
    transporter.sendMail(message, function (err) {
      if (err) {
        console.log('Error during trial email with server error report');
        console.log(err);
      }
    });
  }
  
  var child = new (forever.Monitor)('app.js', {
    outFile: './logs/stdout.log',
    errFile: './logs/stderr.log'
  });
  
  child.on('stderr', function (data) {
    error += data;
  });
  
  // Check for errors every second
  setInterval(function () {
    if (error) {
      sendMail('Your server crashed', error);
      error = '';
    }
  }, 1000);
  
  child.start();
  </code></pre>
  
  <p>I called this file <code>start.js</code>. All it does is: spawns new process (<code>app.js</code>, which runs my Express web-server) with <code>forever-monitor</code>, I listen for <code>stderr</code> event of this child process and collect all errors into variable, then I check this variable every second and if it doesn't empty I send an email and clear variable. This code works fine.</p>
  
  <p>Now to start my server I need execute something like <code>nohup node start.js &amp;</code>. The bad thing that there is no easy way to stop or restart my server. Previously I did it with <code>forever stopall</code> and <code>forever restartall</code> which is very convenient.</p>
  
  <p>My next thought was try to combine <code>forever</code> and <code>forever-monitor</code> so I tried to start server like this <code>forever start start.js</code>. Server started just like I supposed. But restarting didn't work anymore. Executing <code>forever stopall</code> and then <code>forever start start.js</code> didn't work too. Server started to complain that port is already busy. I assume it happens because <code>forever</code> stops <code>start.js</code> process, but the child <code>app.js</code> process spawned by <code>forever-monitor</code> still was occupying the port.</p>
  
  <p>So my question is: how to run Node.js server, receive emails about it's crashes, automatically restart it when it crashed and have possibility to easily <code>start</code>/<code>stop</code>/<code>restart</code> it like with <code>forever</code> command line utility? Solution can use <code>forever</code>, <code>forever-monitor</code> or both of them, or actually any library you think may work for this kind of problem.</p>