SMTP PM2 notifier
Switch branches/tags
Nothing to show
Clone or download
soyuka Merge pull request #8 from l-vo/fix_perpetual_timeout_problem
Fix perpetual setTimout/clearTimout problem
Latest commit 6ce5ab9 Jul 4, 2018
Failed to load latest commit information.
test First commit Jun 12, 2015
.gitignore First commit Jun 12, 2015
LICENSE First commit Jun 12, 2015 Add max_polling_time Jul 4, 2018
config.example.yml Add max_polling_time Jul 4, 2018
index.js Add max_polling_time Jul 4, 2018
package.json 1.0.2 Jul 5, 2017 First commit Jun 12, 2015

How does it work?

We're listening on pm2 bus automatic events (manual exits won't be mailed). When an exit event happend we're keeping the pm2 process environnement in a queue. Those elements are then emailed through SMTP by using the famous nodemailer.

Templates can be customized, if you want to use another mailing protocol feel free to dig into the code! This is meant to be used out of the box with minimal configuration but it can't fit everyone needs.

If you need better email interactions have a look at Keymetrics!


    from: ""
    to: ""
# see
    host: ""
    port: 25
# Events list:
# - restart
# - delete
# - stop
# - restart overlimit
# - exit
# - start
# - online
    - exit
template: './'
# this is the process subject if there is only one event to be mailed
subject: '<%= %> errored (<%= process.NODE_ENV %>)'
# if multiple events are going to be mailed, use a global subject:
multiple_subject: 'Error on <%= hostname %>'
#wait for 5 seconds after the last event before sending an email - avoid spam when a lot of events happened
polling: 5000 
#if events are sent continuously, an email will be sent after 5 minutes anyway
max_polling_time: 300000
#attach your process logs to the email
attach_logs: true 


This is how a standard pm2 object looks like:

{ event: 'exit',
  manually: false,
   { name: 'throw',
     vizion: true,
     autorestart: true,
     exec_mode: 'fork_mode',
     exec_interpreter: 'node',
     pm_exec_path: '/usr/share/nginx/www/pm2-notify/throw.js',
     pm_cwd: '/usr/share/nginx/www/pm2-notify',
     instances: 1,
     node_args: [],
     pm_out_log_path: '/home/abluchet/.pm2/logs/throw-out-0.log',
     pm_err_log_path: '/home/abluchet/.pm2/logs/throw-error-0.log',
     pm_pid_path: '/home/abluchet/.pm2/pids/',
     NODE_ENV: 'production',

     ... //your env variables here

     status: 'stopped',
     pm_uptime: 1434096604224,
     axm_actions: [],
     axm_monitor: {},
     axm_options: {},
     axm_dynamic: {},
     vizion_running: false,
     created_at: 1434096545628,
     pm_id: 0,
     restart_time: 19,
     unstable_restarts: 0,
     started_inside: false,
      { locked: false,
        metadata: {},
        started_at: null,
        finished_at: null,
        error: null },
     versioning: null,
     exit_code: 1 },
  at: 1434096607294 }

You can use all those variables in the template. We're adding the hostname and we format at in a date variable by using date = new Date(at).toString(). Templating is done through lodash and the email should be markdown-formatted.


Mail testing is hard to automate. There is a sample throwing process in the test directory. To test manually just launch pm2 start test/throw.js with node index.js. If you have an idea to unit test this feel free to open an issue.