I am making my first app in node js and i uploaded it on server. I have a terminal where I am running a session containing two panes, one for running mongod and second for running node app.js . Now both of them run as long as I keep it running. But how can we make sure after quitting the window it keeps running both of them mongod and node. I am using tmux, mongodb, express and node.
I tried tmux detach and tmux attach. They work. But for tmux detach first i have to quit the current command by ctrl + c and then i would be able to run tmux detach. Am I doing something wrong? Please Help
I presume this is for a development environment and not production. In production, keeping something running "forever" makes using a process supervisor to make sure it is restarted when it crashes.
In development, you can use <Prefix>-d to detach without cancelling the current command. The Prefix for Tmux is Control-B by default.
Related
So, I can get the directory of dump.rdb's location to change by using the dir option in redis.conf when I start it normally (just calling redis-server). If I want redis-server to run all of the time (I do) without needing a terminal window always open, I think I need to daemonize it. However, it doesn't seem this ever persists to the disk automatically and whenever the redis-server process ends (I've been ending it in testing by just running redis-cli shutdown or sometimes just killing the process with kill PID) and starts back up, all database changes are lost, which seems pretty bad if a crash or unexpected shutdown were to happen in the future. In the code that runs the processing of data (either python with redis-py or java with jedis), I can explicitly run bgsave(), but that saves dump.rdb in the directory that the code was run in and not where the dir option specifies in redis.conf
So, is there either another way to run redis-server without requiring a whole terminal window to stay open that allows what I want to do or is there a way to get the data to persist on disk in the proper directory when it's run as redis-server --daemonize yes or similar?
You could put it on linux "background" using nohup. It does not need a terminal window to stay up and running. I don't know the daemonize option to give you an advice about that, but, see if it works for you:
nohup redis-server &> redis.log&
or
Set daemonize yes in the conf file and run:
redis-server path/to/redis.conf
I have an ubuntu server running with 2 node websites on it and I keep closing the terminals that start the app so I cannot shut the app down to make changes, I end up restarting the server.
How can I see what node instances are running and then stop them, or how do I stop a node instance through programming and I'll just make a button that kills the node instance. Either way...
If you dont want to use PM2 or systemd you can get the list of all the node.js instances running using
ps -aux | grep node
once you have the list of all nodejs processes you can kill any process using kill command
kill "Process ID goes here"
Use a process manager. PM2 is the most widely recommended.
npm install pm2#latest -g
then
pm2 start app.js
Alternatively you can use something like screen to have multiple terminal sessions running in one window
I'm using Amazon WS to test some rudimentary nodejs server. The problem I'm having is that when I close the putty command prompt on my PC, that I can not reach the server anymore with a browser.
Ive read about forever and forever-monitor. I'n not sure why the script must be restarted constantly, but ok let's assume it must.
I'm using both
forever "/home/ec2-user/myApp.js"
and
node "/home/ec2-user/foreverMonitor.js"
(The latter has the myApp.js reference in the foreverMonitor.js file. Similar to Where place forever-monitor code?.)
Both do start the server, but when I close putty, both also let the server die.
What am I missing here?
------------------------------------- update -------------------------------------
I guess I can also skip foreverMonitor (not verified yet)
nohup forever "/home/ec2-user/myApp.js" &
forever stop "/home/ec2-user/myApp.js"
------------------------------------- update -------------------------------------
working, now using this
nohup forever "/home/ec2-user/foreverMonitor.js" &
forever stop "/home/ec2-user/foreverMonitor.js"
I'm not totally familiar with AWS, but it seems that you probably need to run nohup. The trailing ampersand should give you control of the terminal again immediately after executing the command.
$ nohup forever "/home/ec2-user/myApp.js" &
$ nohup node "/home/ec2-user/foreverMonitor.js" &
See this answer for more details on nohup and the trailing ampersand: https://stackoverflow.com/a/15595391/498624
Have a look at PM2 https://github.com/Unitech/pm2
After using forever successfully, I switched to PM2.
forever works fine but I found PM2 was a better fit to my mental model. PM2 also has a very neat (and repidly evolving) Web interface where you can monitor and control node instances. As a bonus you can also run non-node tasks under PM2
I'm new to aws and recently I have been able to install node, mongod and also, FTPed project file to the server.
For mongodb,
I'm doing mongo in a separate terminal tab and starting service in another tab. I want to know how can I keep the mongo service running.
For node app,
Right now i'm doing node app in the server. How can I keep it alive too ?
Now the problem, is I open the browser with publicip:portno but nothing happens. How can I locate app and run it in the browser.
my app structure is
/
node
mongo
server.js and app related files
Using the Linux/Unix nohup command allows you to start commands that ignore the signals associated with the controlling terminal process terminating (SIGHUP). Adding the & to the command allows that command to run in the background and sending the output to /dev/null will ensure that your disk does not fill up with unnecessary log output. Here are some commands that should work:
nohup mongod >/dev/null &
node server.js >/dev/null &
This is more of a Linux/Unix command line issue I think. You can use the node module called forever to run your Node.js process in the background easily.
npm install -g forever
forever start YourScript.js
You can place an & at the end of the mongod command to place it in the background.
Ensure that in your node app you have the command app.listen("port number");
This is the "port number" that you should be using in your browser to render the page with the elastic IP from your AWS instance. Make sure that your elastic IP is configured to accept inbound requests.
To keep the service/app running in the background you can run the screen command then launch ur app/service (e.g. mongod OR node app.js). In the same terminal that your app is running press control + a + d, you should see
(detached)
printed on your screen.
This should keep your app/service running in the background.
As you can tell by my question, I'm new to this...
I built my first website, I set up my first Node.js server to serve it and then pushed everything live on EC2.
I tested everything on my EC2 IP address and everything seems to be working.
Now up until now, I've been testing my app locally so it makes sense that whenever I closed the terminal, app.js would stop running so nothing would be served on localhost.
Now that my server is on EC2, the same thing happens ("obviously" one could say..) whenever I close my terminal.
So my question is how do I keep my Node.js server running on EC2 for like... forever..so that my site stays live.. forever :)
I read something about a node module called "forever" but I'm wondering (being new and all..) why isn't this "forever" functionality a default setting of the Node.js-EC2 system ?
I mean, correct me if I'm wrong, but isn't the whole point of setting up a web server and pushing it live to have it stay live forever? Isn't that what servers are supposed to do anyway (infinitely listening for requests) ? And if that's the case why do we need extra modules/settings to achieve that ?
Thanks for your help.. As you can tell I'm not only looking for a solution but an explanation as well because I got really confused.. :-)
EDIT (a few details you might need) - After installing my app on EC2 these are the steps that I follow on the terminal (The app is running on Amazon Linux by the way) :
I type ssh -i xxxxxxxxxxx.pem ec2-user#ec2-xx-xx-xx-x.eu-west-1.compute.amazonaws.com on the
terminal
After logging onto the Amazon machine I then go to the relevant folder and execute node app.js
There are 3 folders in the machine : node, node_modules and *name of my app*
app.js resides in *name of my app*
After that, the site goes live on my EC2 IP
Once I close the terminal, everything is switched off
Before you invoke Node.js, run the command:
screen
This will create a persistent environment which will allow your process to keep running after you disconnect.
When you reconnect, you can use this command to reconnect to that environment:
screen -r
Here's a random link to learn more about screen:
http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
However, this won't help you if your EC2 instance restarts. There are many different ways to do that. Adding your startup command to /etc/rc.local is one way. Here's a link to an Amazon guide which includes adding something to /etc/rc.local.
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/building-shared-amis.html
I worked with the valid answer for a while but some times the screen just end with no reason also screen has no balance loader and others features that in a production enviroment you should care , Currently I use a npm component to do this job.
https://www.npmjs.com/package/pm2
This is so easy to use.
$ npm install pm2 -g
then just start your app with pm2 like this
$ pm2 start app.js
In the above link you can find diferents tasks to perform if you need.
Hope this help the newbies like me.
There's a better way. Use forever.js.
See it here: https://github.com/foreverjs/forever
This is a nice tutorial for how to use chkconfig with forever on CENTOS.
http://aronduby.com/starting-node-forever-scripts-at-boot-w-centos/
Or use tmux
Just Enter a tmux screen run node server
Ctrl+b Hit D and you're done.
I am very late to join the thread and seems its basic problem with every newbie. Follow the below to setup properly your first server.
follow the step on the ec2 instance(before doing this make sure you have a start script for pm2 in your package.json file):
npm install pm2 -g
pm2 startup systemd
See the output and at the last line it must be like..
You have to run this command as root. Execute the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup
systemd -u sammy --hp /home/sammy
Take the last line command and run again with root privilege.
(before running the next command, Provide a new start script for pm2 in your package.json file e.g: "pm2-start": "pm2 start ./bin/www")
npm run pm2-start
for more info follow the link.
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
If you are using a Ubuntu EC2, better to use the following we have been using this for the past 6 years and have had no issues with this.
sudo npm i -g forever
Now start your main, example
forever start index.js
forever start src/server.js
To stop the server use the following command
forever stop index.js
To list multiple servers running forever
forever listall