I've installed node on my ubuntu virtual private server and I SSH into it.
I run
$nodejs index.js
but after running the file, I'm now stuck in the node command prompt and I can only get out by pressing ctrl-C. But doing that stops my nodejs server.
I want to run my server and then go back to the shell and do other things. How do I do this?
Use PM2. It is a process manager. You can do whatever after initiate this on the server.
https://github.com/Unitech/pm2
Related
I want to ping certain IPs in a project, and I am using npm ping package for this.
https://www.npmjs.com/package/ping
When I run the service, its running fine. But when is run my service in the PM2, the command prompt windows are showing up, each time the ping action is done in that service(I am pinging in loops to monitor an IP).
What is the reason for this and please suggest an alternative solution.
The same problem happened to me while using forever to run my script in background.
I've found that using START /B node script.js on windows instead of forever worked just fine and the command prompt didn't showed up. I don't know much about PM2 but maybe you should be able to prefix your command with START /B somewhere in PM2 or just run your script by yourself.
I run a angular app like this:
npm run server-java
From a terminal window.
The server starts, but I want it to give back the shell prompt.
I want the shell prompt back while it runs.
You can use tools like pm2 or forever to start your nodejs app as a background process. Usually used for production setup.
Another option is:
npm run server-java > /dev/null 2>&1 &
You will get back to the terminal, but then you have to kill process manually by id when you don't need it.
I'm using grunt to run the MEAN project on Ubuntu, but when I close the putty (I use putty to connect Ubuntu server from my PC), it would close the program too.
My question is how can I keep MEAN running?
Update: nohub grunt & stops after I close putty
There are various node based process managers which can serve your task. My favorite is pm2 (http://pm2.keymetrics.io/)
Package managers allow your program to keep running even in case of hiccups. They can watch your project directories for any changes that you might push to them and restart servers based on those changes.
Other favorite is forever (https://www.npmjs.com/package/forever).
you need to run the command in background and I would also recommend to use nohup so:
nohup grunt &
should do the trick.
https://en.wikipedia.org/wiki/Nohup
NODE_ENV=staging nohup node appStag.js &
You can use the above command to run node server
and you can get the above environment using process.env.NODE_ENV
I found a npm package called forever is a good solution, I use forever to run the program right now; and it works perfect with putty.
I installed the .msi file from nodejs website and installed it.
Now, when I run nodejs.exe, I do get a command prompt, but it shows a blinking > by default, instead of C:/>
It looks somewhat like this:
What to do?
This is called the REPL. You can enter statements for Node to execute and get realtime feedback. Ctrl+C twice will get you back to the command prompt.
I recommend checking out the answers to How do I get started with Node.js for learning more about Node.js and how it works. Typically you provide a file with your Javascript for Node to execute:
node app.js
or you can leave the .js off:
node app
I am running node using putty. but it doesn't work when putty session expire. How can I start node js permanently, should not be terminated if putty session end.
I just solved that issue yesterday using Forever > http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever
It's awesome.
For Installing: npm install Forver
For running: forever start yourServer.js
For checking if its running: forever list
cool eh?
Generally you use a deamon to keep it running. A proper answer depends on what type of OS your remote machine is running (windows ?).
It's best to run node.js on *unix.
Among what Raynos and nEEbz are suggesting you can also try to use GNU Screen. This is very handy especially if you are using putty to connect to remote server. Check out this screen tutorial for more information.
Here is a quick and gentle introduction to "screen" .
In ubuntu, if you need to install it, use: apt-get install screen
First use:
$ screen
bunch of stuff prints out, then another shell prompt
$ node ./myapp.js
now your node app is running
You want to edit some other code?
control-a c
the window clears, and you now have another shell prompt. node is still running....
$ edit public/somewebfile.html
save it, still in editor
go back to node
control-a control-a
screen switches back to the screen running node
need to leave the office for a few minutes
control-a control-d
(screen detaches from your location... processes remain attached)
$ logout
screen disconnects, but nodejs and the editor are still running...
back at home... want to connect to work
> ssh work.some.where
> screen -D -R
screen reconnecting....
now you see the nodejs shell screen again, or control-a control-a to switch back to that editor
control-a ? shows available commands, or read the nice man page: man screen
You can also run nohup:
nohup node app.js
If you get an error when you do npm install Forever, do npm install forever -g
I got that problem.
https://www.npmjs.com/package/forever
Use PM2
just install pm2 on the server and run app like this : pm2 start app.js
you can also monitor your application from the web panel
PM2 (proccess manager)