12

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,

Lee
  • 5,816
  • 6
  • 45
  • 61
  • How does this have anything to do with django? – Games Brainiac Aug 04 '13 at 12:27
  • Its the Post destination – Lee Aug 04 '13 at 13:35
  • What does `nohup.out` show. How long does it take before your app gets killed. – user568109 Aug 06 '13 at 15:11
  • You have to find and give some details here, there is really not much info. Also how are you testing the server. It does not respond to HTTP requests, only websocket connections. Could it be that server did not die but not responding. You have disabled info and warning with `io.set('log level', 1);`. To get some trace comment that line. – user568109 Aug 07 '13 at 04:57
  • Well this is the issue.. I've had no feedback to work with.. The process just terminates.. Nothing entered into a log or any of the various trace/debug methods. I have been at a loss myself. I have changed the log level aswell, nothing. – Lee Aug 07 '13 at 08:51
  • Which shell are you using ? bash/csh/etc. Try to tun the app like this : `nohup node app.js > output.txt 2>&1 &` with the earlier changes. It will create `output.txt`, so paste it in your question. Also are you taking any input from app.js, username/password etc. – user568109 Aug 08 '13 at 05:34
  • Did you try to without nohup ? What is the behavior ? I tried the same code in windows and i did not have any problems.The program is alive even after an hour. – Chandu Aug 08 '13 at 18:39
  • Yep, tried with Forever and just with nohup. Also it is dieing when run from the terminal also.. What is interesting is that i have been running it as the root user for the past day and it has not died.. I am wondering if the server or maybe even another process like CPanel is actually monitoring processes and killing things it doesn't like. – Lee Aug 08 '13 at 20:50
  • If it works when you run is as root - could be something with access permissions, although unlikely. Have you tried running this in a different environment? – diversario Aug 10 '13 at 04:06
  • 2
    You might try `screen` instead of nohup. screen runs from the shell, just screen. Then you are in a new shell, except terminal output is redirected in a potentially useful way. You can detach screens with control A control D and the processes run inside keep going, and have a virtual terminal to print errors or information on. You can then reattach later from a different session with screen -R – Paul Aug 11 '13 at 09:37
  • Also, my suspicion is that no one can remote debug this as shown unless it is a "I had the same problem" type of answer. In that case, one approach would be to add a bunch of console.log statements and try to understand how each connection is being handled. That may tell you if it is dying when handling a connection, or perhaps specific data is being received or processed when it fails... – Paul Aug 11 '13 at 09:40
  • Have you tried running your node.js app on another machine ? The node process might be killed by the OS for some reason. – Aurélien Gasser Dec 10 '13 at 03:00

5 Answers5

4

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.

Varun Oberoi
  • 682
  • 6
  • 20
1

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

ilmatic
  • 565
  • 3
  • 11
1

Run the process using forever or pm2 both are awesome modules, they will give proper back trace where it got failed.

Kishorevarma
  • 936
  • 6
  • 23
0

use nohup node app.js >> log.txt to get the exception in a log file

rob_james
  • 1,262
  • 1
  • 12
  • 17
-1

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
Daniel
  • 9
  • 3
  • You don't seem to understand what nohup does, you don't explain why you think it's bad, and you suggest a method that is both overcomplicated and is prone to the problem that nohup is intended to solve. – wfaulk Oct 30 '13 at 23:15