how to run node js remotely - node.js

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)

Related

How can i start node typescript process (Express API) in background?

I have a express API i need to start this process in background to continue when i close the connection (Ubuntu Server - Terminal)
node --inspect -r ts-node/register server.ts
Thanks
In regards to NodeJS applications, check out PM2 to run, and automatically restart your application in the event of a crash or system reboot.
npm install -g pm2
pm2 start server.js
pm2 save
pm2 startup
Generally in production, you should be utilizing your transpiled JavaScript, instead of TypeScript directly, hence my use of server.js, but nothing stops you from integrating TypeScript with PM2.
For a more broad answer, screen can be used to run processes in the background. This should be installed by default, if not run: sudo apt install screen
Using screen:
Run screen -S <session_name> to open a new session, press SPACE to push through the licensing gibberish.
Start the desired processes in your screen session.
Press CTRL A then CTRL D to detach from the session; this will continue to run even if you close your terminal.
Run screen -r <session_name> to return to your screen session, screen -list lists all active screen sessions.
Press CTRL D or run exit in a screen session to terminate it.
View the manpages for more details.

How do I keep my discord bot online even with my terminal closed

I have a pretty simple discord.js bot and would like to keep it running even when I close my terminal and/or my computer.
I have already tried pm2 and just keeping my terminal open throughout the day, but it wastes battery.
I keep running pm2 run NAMEOFFILE.JS in the right folder, but it already says I'm running the file.
You must use a process manager, like pm2 (pm2 start yourfile.js) or forever (forever start yourfile.js). You can install them with npm (npm i -g pm2, npm i -g forever).
EDIT: Yeah use PM2 or some other application, screen is a bad idea.
If you're on either MacOS or Linux, you can use the screen command (Or cygwin on Windows). It basically creates another terminal. To use it you can do screen -R <name>.
When you use the -R <name> (capital) the computer will look for a screen with the name and reattach to it. If it can't find a screen with that name it will create a new screen with that name.
So for you, you might do screen -R Discord. After you attach to the screen you can do whatever you want as if it were a terminal. When you're done, you can press ctrl+a then d to detach from the screen, then you can close the terminal window.
https://ss64.com/osx/screen.html

How to run a node.js file as a background process on a Windows server?

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.

How to keep MEAN (stack) running?

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.

How do I exit the Meteor development server on Mac (CTRL-C equivalent)?

I have a project in Meteor that I started in Windows but have now migrated over to OS X. In Windows, I was always able to stop running the project locally simply by using CTRL-C. This doesn't seem to do anything in the terminal in OS X, it just brings up a new command line.
When I type meteor reset, I get the following:
reset: Meteor is running.
This command does not work while Meteor is running your application. Exit the running Meteor development server.
I looked at meteor --help but no luck there. I also saw a post recommending I go into Activity Monitor and shut down any node processes (which didn't help) but there has to be a way to do this directly from the terminal.
I'm stuck and can't reload the app, because I can't shut down the previous one. Thanks.
Use this code to kill the meteor process:
kill `ps ax | grep '[m]eteor' | awk '{print $1}'`
in Webstorm, click the red cross icon on the left side bar to close the terminal session.

Resources