Run `node server` command in Ubuntu 18 on startup and reboot - node.js

I've installed the Adapt Authoring tool on an AWS Ubuntu 18 instance. It is started up with a node server command. I've looked into pm2 and some other solutions. I can start the process using pm2 start 'node server', but pm2 does not list the process so I can't save it... I've also looked at upstart, which seems possibly outdated. I tried to add a process using update-rc.d to no effect. I also tried adding a service to /etc/systemd/system/. No go. None of these solutions seem to work with this startup command.
What is the solution I am supposed to be using for this?

Looks like pm2 will work, but it needs to be run like so: pm2 start 'node server'. Then it will run, list, and save the process.

Related

npm ping is opening command prompt windows

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.

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.

Node.js - How do I detect if a node script is running?

I want to write a node script that detects if another node script is running.
My question is (all running on node in Windows 7), within a node script, how do I detect if another node script is running?
I'm new to node and I couldn't find an answer that matched my question exactly.
I'd be tempted to use nodemon...
Once installed (with npm install -g nodemon), instead of starting your server with say node app.js - start it with nodemon app.js.
Nodemon will listen for any changes to your files, and restart your server automatically for you. Perfect for development!
If this, for some reason, is not an option though, you could use something like the taskkill command to kill all the node processes with something like taskkill /F /IM node.exe
I'm not a Windows Guru though, so there's will likely be a better way of doing this with something like a batch script or windows service (rather than just killing ALL node apps)
Perhaps someone on StackOverflow with more Windows experience can help out here.
Good luck!

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 to make a node application on Digitalocean run constantly?

I recently got a node application up and running on digitalocean's droplet. Everything is working properly, if I go forever on the server javascript file I can navigate to the site and it works.
If I close the terminal window in which I am ssh-ing, the site is no longer available, which is expected of course. But my question is, how can I run the forever as a daemon of sorts?
I could not find the answer anywhere, which is really strange...hope somebody here might be able to help me
Generally forever should continue running if you set it up as a background task and don't close it. Unfortunately if the server needs to restart (which can happen often), you need something to get it started again.
An alternative to forever is pm2, pretty much the same thing. There is a tutorial on how to set up your production server here. Here is an excerpt from the site for just the pm2 part, as I assume you got everything else sorted:
Install PM2
Now we will install PM2, which is a process manager for Node.js applications. PM2 provides an easy way to manage and daemonize applications (run them as a service).
We will use Node Packaged Modules (NPM), which is basically a package manager for Node modules that installs with Node.js, to install PM2 on our app server. Use this command to install PM2:
sudo npm install pm2 -g
Manage Application with PM2
PM2 is simple and easy to use. We will cover a few basic uses of PM2.
Start Application
The first thing you will want to do is use the pm2 start command to run your application, hello.js, in the background:
pm2 start hello.js
This also adds your application to PM2's process list, which is outputted every time you start an application
PM2 automatically assigns an App name (based on the filename, without the .js extension) and a PM2 id. PM2 also maintains other information, such as the PID of the process, its current status, and memory usage.
Applications that are running under PM2 will be restarted automatically if the application crashes or is killed, but an additional step needs to be taken to get the application to launch on system startup (boot or reboot). Luckily, PM2 provides an easy way to do this, the startup subcommand.
The startup subcommand generates and configures a startup script to launch PM2 and its managed processes on server boots. You must also specify the platform you are running on, which is ubuntu, in our case:
pm2 startup ubuntu
The last line of the resulting output will include a command (that must be run with superuser privileges) that you must run:
Output:
[PM2] You have to run this command as root
[PM2] Execute the following command :
[PM2] sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u sammy
Run the command that was generated (similar to the highlighted output above) to set PM2 up to start on boot (use the command from your own output):
sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u sammy

Resources