How to keep a server nodejs running permanently?

Asked

Viewed 9,800 times

10

I want to know how to maintain my server permanently running on my Ubuntu I bought without the need to run Putty and run the command node app.js. I made a REST API that will be consumed by a mobile application so the server should always be running.

3 answers

10


You can use the nohup:

$ nohup node server.js > output.log &

This way, you can drop and the server will remain active.


To run "forever", I recommend using the Forever.

$ npm install forever
$ forever start server.js

To see the servers running, use the option 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 (in English).

  • Excellent! Thank you.

1

0

You can make 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 boots again.

Example of cron

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

Example of 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 in this python file to generate logs of when Node is initialized.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.