Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a NodeJS process on a remote server?

Tags:

linux

node.js

I've created a simple NodeJS app which I have now moved up to a server in AWS.

I'm able to ssh into the server and start the application but obviously as soon as I close the terminal the process stops.

How to I start my NodeJS app and keep it running after I've closed my terminal?

like image 568
michael Avatar asked Jan 22 '26 02:01

michael


2 Answers

That's not a specific nodejs problem, although there are specific solutions (for example forever).

A generic solution to remotely start any program on linux and not have it die when you close the session is to launch it using nohup. Here's an example in which I launch node and redirect both the standard and error outputs into the server.log file :

nohup node main.js >> server.log 2>&1 < /dev/null & 
like image 118
Denys Séguret Avatar answered Jan 24 '26 19:01

Denys Séguret


Like Denys Seguret said, this can be done using the forever node package. Here's how -

SSH into your server and install forever globally as root level user

sudo npm install forever --global

To run forever on a a node script, execute in terminal while in same directory as your server.js like this -

forever start server.js

Make sure to test if its working by launching your browser at the appropriate url. If you want to check the status of all of the forever scripts you have running on your server (you can run it on multiple scripts) run this command -

forever list

and to stop a forever script you can either run forever stop which will stop all your forever scripts I believe, or run forever stop 0 where 0 is the index of your script in the forever list.

I use forever on all my gulp applications, haven't had any problems yet, but I don't think I have had any major shut down issues with my remote server. Maybe if that happens I'll have more to say about the credibility of forever. Some credits for this answer from this website.

like image 27
kiko carisse Avatar answered Jan 24 '26 17:01

kiko carisse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!