I am writing my first NodeJS app but for some reason it seems to die unexpectedly after a short amount of time. I have no clue what would be causing it. The process runs fine, even works as expected, then for some reason it just stops. The nohup log does not show an error or any feedback.
I have tried running this in debug mode but its the same, no information. The trace is no help.
I run the process via nohup:
nohup node app.js &
Code:
var http = require('http');
var server = http.createServer().listen(8000);
var io = require('socket.io').listen(server);
var cookie_reader = require('cookie');
var querystring = require('querystring');
// Store the session cookie set by Django
io.configure(function(){
io.set('authorization', function(data, accept){
if(data.headers.cookie){
data.cookie = cookie_reader.parse(data.headers.cookie);
return accept(null, true);
}
return accept('error', false);
});
io.set('log level', 1);
});
io.sockets.on('connection', function (socket) {
socket.on('shoutbox_send', function(message){
values = querystring.stringify({
comment: message,
sessionid: socket.handshake.cookie['sessionid'],
});
try {
var options = {
host: 'www.example.com',
port: 80,
path: '/shoutbox/node_api',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': values.length
}
};
var req = http.get(options, function(res){
res.setEncoding('utf8');
res.on('data', function(message){
socket.emit('shoutbox_receive', message);
socket.broadcast.emit('shoutbox_receive', message);
});
});
req.write(values);
req.end();
} catch (err) {
console.log(err);
socket.emit('shoutbox_receive', err);
}
});
});
Thanks in advance,
If you run your app with Forever.js, it will have 2 effects that might be helpful for you:
(1) it will automatically restart your app when it terminates
(2) it should give you an exit message like
warn: Forever detected script exited with code: [EXIT CODE]
which should make it easier to track the issue down
Run the process using forever or pm2 both are awesome modules, they will give proper back trace where it got failed.
I faced a similar problem ( but don't know whether there is a common reason )
Solution :
nohup node app.js & as normalLast point is important I was not exiting from the ssh correctly hence it was getting killed.
use nohup node app.js >> log.txt to get the exception in a log file
You shouldn't run it this way. The definition of nohup process is:
nohup - run a command immune to hangups, with output to a non-tty
Also, nohup changes from one shell to the other, basically it means that you won't get the same behavior for this process on different linux distros.
You should run it as I'm doing on our servers in my company:
/usr/local/bin/node app.js > /var/log/node_js.log 2>&1
then press:
Ctrl-Z
and then enter:
bg
The process will run in the background and output its error and output logs to /var/log/node_js.log. In case it will fail you'll see the failure reason in that file.
You can leave regularly the ssh session you have to the server using:
exit