Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs nohup dieing without an exception

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,

like image 308
Lee Avatar asked Aug 04 '13 11:08

Lee


2 Answers

I faced a similar problem ( but don't know whether there is a common reason )

Solution :

  1. ssh to the server
  2. run nohup node app.js & as normal
  3. Ctrl+c to exit from nohup
  4. Gracefully exit from ssh i.e via exit command not by just closing the terminal.

Last point is important I was not exiting from the ssh correctly hence it was getting killed.

like image 190
Varun Oberoi Avatar answered Sep 17 '22 04:09

Varun Oberoi


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

like image 23
ilmatic Avatar answered Sep 21 '22 04:09

ilmatic