How to maintain a running nodejs server?

7

I want to know how to keep my server in permanent implementation in my Ubuntu I bought without the need to have it run the Putty and run the node app.js command. I made a REST API that will be consumed by a mobile application so the server should always be running.

    
asked by anonymous 19.02.2014 / 15:17

3 answers

7

You can use the nohup :

$ nohup node server.js > output.log &

So you can move and the server will remain active.

To run "forever", I recommend using Forever .

$ npm install forever
$ forever start server.js

To see the servers running, use the list

$ forever list

The good thing about Forever is that it automatically restarts your server if, for some reason, it dies.

For more details, see this link .

    
19.02.2014 / 15:20
1

You can do a cron that runs a python file that will keep your application running at all times. The good thing about using cron is that it will ensure that the node server is rebooted again.

Example cron

*/1 * * * * /usr/bin/python /home/cron/vp.py

Sample vp.py file

import os
import commands
saida = commands.getoutput('ps -A')

# verifica se existe o node nos processos retornados por ps -A
if 'node' in saida:
        pass
        # print "Node em execucao..."
else:
        # se nao existe, entao executa o servidor node em segundo plano...
        os.system('node /usr/bin/server_chat.js &')

If you want, it is interesting to put this python file to generate logs when the node is initialized.

    
23.02.2014 / 18:01
1

Redirect the flow to a log file

node app.js > log.txt &

To make it even better, create a service to start the api:

    
26.09.2016 / 18:26