Is there a configuration or flag to restart the node process every hour?
Look at the cron_restart, app will be restarted at 01minute 01second of every hour.
{
"name": "NAME",
"script": "./path/to/app.ks",
"cron_restart": "01 01 * * * *"
}
Hmm! Thanks. Doesn't seem to work though. I actually waited for it using the time log. My processes.json looks like this:
{
"apps" : [{
"name" : "app",
"script": "./app.js",
"watch" : "true",
"cron_restart": "01 01 * * * *",
"log_date_format" : "YYYY-MM-DD HH:mm Z"
}]
}
Does it work for fork_mode?
It only works in cluster_mode:
{
"apps" : [{
"name" : "app",
"script": "./app.js",
"watch" : true,
"exec_mode": "cluster_mode",
"cron_restart": "01 01 * * * *",
"log_date_format" : "YYYY-MM-DD HH:mm Z"
}]
}
You can also use pm2 programmatically :
var pm2 = require('pm2');
pm2.connect(function(err) {
if (err) throw err;
setTimeout(function worker() {
pm2.restart('app', function() {});
setTimeout(worker, 60 * 60 * 1000);
});
}, 60 * 60 * 1000);
});
Thanks
I put the above code in pm2.js and started it with pm2 start pm2.js, but I'm getting this in the logs:
app-1 (err): 2015-03-11 16:08 +02:00: events.js:72
app-1 (err): throw er; // Unhandled 'error' event
app-1 (err): ^
app-1 (err): 2015-03-11 16:08 +02:00: Error: listen EADDRINUSE
app-1 (err): at errnoException (net.js:905:11)
app-1 (err): at Server._listen2 (net.js:1043:14)
app-1 (err): at listen (net.js:1065:10)
app-1 (err): at Server.listen (net.js:1139:5)
app-1 (err): at EventEmitter.app.listen (/opt/node_modules/useraccountservice/node_modules/express/lib/application.js:595:24)
app-1 (err): at Object.<anonymous> (/opt/node_modules/useraccountservice/user/application.js:161:18)
app-1 (err): at Module._compile (module.js:456:26)
app-1 (err): at Object.Module._extensions..js (module.js:474:10)
app-1 (err): at Module.load (module.js:356:32)
app-1 (err): at Function.Module._load (module.js:312:12)
app-1 (err): at Module.require (module.js:364:17)
app-1 (err): at require (module.js:380:17)
app-1 (err): at Object.<anonymous> (/opt/node_modules/useraccountservice/app.js:1:80)
app-1 (err): at Module._compile (module.js:456:26)
app-1 (err): at Object.Module._extensions..js (module.js:474:10)
app-1 (err): at Module.load (module.js:356:32)
I ran pm2 start pm2.js after running pm2 start processes.json. Any ideas please?
cron_restart doesn't work for me too.
pid doesn't change at minites.
{
"name": "realname",
"cwd": "/home/test10/realname",
"env": {
"NODE_ENV": "test",
"DEBUG": "noradle:*"
},
"exec_interpreter": "node",
"script": ".",
"args": [
"8011:localhost",
"9015"
],
"log_date_format": "YYMMDD(d) HH:mm:ss:SS",
"error_file": "/var/log/pm2/test10/realname.log",
"out_file": "/var/log/pm2/test10/realname.log",
"merge_logs": true,
"max_memory_restart": "300M",
"cron_restart": "* * * * *"
}
@kaven276
This is what I ended up using:
var request = require('request');
var pm2 = require('pm2');
var config = require('./config')()
var ping_timeout = 3000;
var pm2_app_name = config.appname;
var worker_interval = 600000;
var hostname = config.uri + '/auth';
var ping_server = function(timeout, callb) {
// Wait, let me check before you say the server is down.
var timer = setTimeout(function() {
callb(false);
}, timeout);
request(hostname, function(err, res, body) {
clearTimeout(timer);
if (err || res.statusCode != 200)
return callb(false);
console.log("Server successfully pinged at: ", new Date())
callb(true);
});
};
pm2.connect(function(err) {
if (err) throw err;
setTimeout(function worker() {
ping_server(ping_timeout, function(ok) {
if (!ok) {
console.log('server down, restarting it');
pm2.restart(pm2_app_name, function() {});
}
else {
console.log('server up');
}
setTimeout(worker, worker_interval);
});
}, worker_interval);
});
Works perfectly.
The solution provided by @jshkurti works great but has a syntax error.
Bugfix:
var pm2 = require('pm2');
pm2.connect(function(err) {
if (err) throw err;
setTimeout(function worker() {
console.log("Restarting app...");
pm2.restart('app', function() {});
setTimeout(worker, 1000);
}, 1000);
});
PM2 can restart an exited process automatically:
pm2 start nodeapp.js --watch
In the app exit the process every hour:
setTimeout(function(){
process.exit(0);
}, 60 * 60 * 1000);
I am using node-cron to achieve this with a slightly modified version of honato11's code:
LISTENER_PROCESS_NAME=hs_listener
RESTART_LISTENER_SCHEDULE='*/10 * * * * * *'
var pm2 = require('pm2');
var cron = require('node-cron');
const restartListener = () => {
pm2.connect(function(err) {
if (err) throw err;
pm2.restart(process.env.LISTENER_PROCESS_NAME, () => {
pm2.disconnect()
});
});
}
const scheduledListenerRestart = cron.schedule(process.env.RESTART_LISTENER_SCHEDULE, () => {
console.log('Restarting listener')
restartListener.default()
})
scheduledListenerRestart.start()
console.log(`${new Date()} Initialised listener restart schedule`)
pm2 3.0.0 (not tested with earlier versions) can be configured with cron_restart not only in cluster_mode, fork_mode (default value) also works for me 馃憤
crontab -e
*/60 * * * * pm2 restart app
That will restart app every hour!
Most helpful comment
PM2 can restart an exited process automatically:
pm2 start nodeapp.js --watch
In the app exit the process every hour:
setTimeout(function(){
process.exit(0);
}, 60 * 60 * 1000);