1

I've a linux server (ubuntu) and installed node.js.

My script: webserver.js:

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(80);
console.log('Server is running');

I'm connecting to the server with putty starting the server script: node webserver.js

It works. But If I close the terminal, the webserver is terminated. How can I solve this?

Sebastian
  • 377
  • 1
  • 2
  • 17
  • possible duplicate of [Node.js as a background service](http://stackoverflow.com/questions/4018154/node-js-as-a-background-service) – moka Mar 19 '14 at 10:41

2 Answers2

1

The simplest way would be start it using nohup in background:

nohup your_server &

This would suffice for testing. If you are searching for a stable production solution then you'll have to damonize the script.

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    Did you mean this one? http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever -> section: Keeping it running with Forever – Sebastian Oct 18 '13 at 17:05
  • yeah, that's exactly what I mean, but the blog article is much more detailed. ;) follow it, that's the way to go. – hek2mgl Oct 18 '13 at 17:06
1

You could also use screen. To install it

apt-get update && apt-get install -yf screen

(I think).

Then, create a new screen using

screen -S nodeserver

(replace nodeserver withwhatever name you want to give to the screen. DO NOT LEAVE THIS BLANK!. Leaving this blank makes it hard to shut down).

Once you are using the screen, it acts like its own terminal. So, just start your server from there. Then, press

Ctrl + a

d

To get out of the screen. The screen will continue to run in the background, and will not disconnect. The upside of this is is that you can later reconnect to the screen using

screen -r nodeserver

(see why I said to make it memorable? Otherwise, it would be assigned a random number, which is harder to use but can be found throw using screen -list )

So, if there were any errors thrown, or you logged something, all of that information would still be availble. Please note that screen does not always play nice if you are using ssh.

If for some reason the screen freezes, then that is a problem, but I think that you can fix it using

screen -S nodeserver -X quit

or something of the like (anyone else, please feel free to change this if this is wrong).

Sbspider
  • 389
  • 5
  • 14