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.
Related
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 was creating a node.js project and uploaded it to my Windows server to provide an API service for mobile application.
When I open command prompt and type
node app.js
It runs correctly, but when I close the command prompt my node.js server stopped running.
How to make it still running when I close the commend prompt?
For example on Ubuntu I use the command
nohup
How can I do this on Windows?
You can make the process run in background using pm2
pm2 start app.js --watch
This will start the process and will also look for changes in the file.
More about watch flag
Nodemon #ftw. On Windows, Forever doesn't really watch files so much as casually observe them, while pm2 restarts the server every time you load a page.
Each of these tools works similarly, and each installs just as in the accepted answer. E.g.:
npm install nodemon -g
This installs nodemon globally, and to use you can simply navigate to your project folder and enter:
nodemon
(Assuming your project has an app.js file). You can add the -w switch, just as in Forever and pm2, however if you're just wanting to watch all files, you can omit that. To run nodemon in the background with Forever, you would run this:
forever nodemon --exitcrash
Nodemon is good for development, especially on Windows, while Forever and pm2 are more for production, on Linux.
Here is a simpler answer that cuts right to the chase without any added libraries or overhead like in the other two answers described above. To run your Node.js application as a windowless startup program in the background (this would be analogous to "nohup" in Linux), modify this template to suit and copy it into a .VBS script file. Then copy that file to your Start Menu startup folder (for all users, that would be C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup) and it will automatically run. The techniques you are using here in Visual Basic are (1) preparing to run the Node.js application by first changing the working directory of the shell object and (2) informing the shell to run the Node.js application in a hidden window by adding a “, 0” immediately after the run function:
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\path-to-your-node-js-app\"
objShell.Run("""node"" your-app.js"), 0
Set objShell = Nothing
References:
https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
https://devblogs.microsoft.com/scripting/how-can-i-change-the-working-folder-of-a-script/
No, you can't.
Even if you make a GUI program you'll need to run it via console.
And as soon as you close the command prompt. Your service would be stopped/ terminated that moment only. Because node creates a server itself while running : http.createServer().listen(port) or app.listen(port). So this this makes it independent in nature.
So, as soon as you close the command prompt on which server was running all the services would stop at that moment.
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'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
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)