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
Related
I am having difficult times, trying to make my NodeJS scripts run on windows server 2012. Or more precisely, to make it robust.
I have installed PM2, whic his great, also added service for windows startup which works fine, but now I found biggest issue I can't solve.
When windows server user start pm2 start, directly on server or through ssh, when logging out, all pm2 scripts are gone.
I've tried to look into pm2-windows-service but that seems inconsistent, when I restart service, it works fine, but sometimes I need to manually reload o restart only 1 script and then whole list of pm2 scripts gets somehow detached or attached to user, so when I log out from server it's all gone again.
I can't find solution to have watcher/autorestart on scripts, and make them run as a service regardless of user being logged in/out.
There must be solution for running multiple nodejs scripts on windows ?
Once you launch your NodeJS processes, you should run pm2 save command. That way, your stated scripts are saved.
Your attached/dettached problems may be due a relative path configuration in the service env variables. Try moving to an absolute path.
Note: PM2 recommendation is to launch commands with an admin-privileged user; otherwise inconsistences may appear.
Note (2): Bare in mind that PM2 on Windows has some issues. For example, if you restart your server, most of the times processes dissapear from pm2 ls (they noted that Windows does not have the feature to restore saved instances). If you saved them, pm2 resurrect will restore them.
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.
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 created a cronjob with sudo crontab -u USERNAME -e and tried to start my node.js app after each reboot.
It had worked well at another server.
But this time, I installed the Node.js via nvm
Then tried to run the same cronjob lines, but every time after reboot it failed. I tried to figure out all the related folders and tried again to run as different lines. When I check with forever list, server shows that no process is running.
#reboot /home/USERNAME/.npm/forever start -c /home/USERNAME/.nvm/current/bin/node /home/USERNAME/APPNAME/app
#reboot /home/USERNAME/.npm/forever start /home/USERNAME/APPNAME/app
#reboot /usr/local/bin/forever start -c /home/USERNAME/.nvm/current/bin/node /home/USERNAME/APPNAME/app
I think that the reason is somehow related with nvm. But I am not sure of that. I don't want to do anything unnecessary unless I am fully sure about it.
Edited July 26, 2015
Though I have used the npm's forever module to deploy nodejs production apps, I really do not see the need to use it on linux based servers as there are so many system level alternatives available.
One of them is upstart. It will help you run your scripts as system level services. Amazon Web Services also use upstart in their Elastic Beanstalk nodejs tiers to keep nodejs apps running forever.
If you really just need to get down to it, here is a link to run your nodejs app as an upstart service.
However, it is not limited to deploying nodejs apps only and if you learn upstart, you will be able to do a lot of things with it. Here is a link for that as well.
If your original approach was preferable (as it was in my case) you can fix your scheduled cron job by explicitly passing the path to node like this: (found in this answer)
#reboot /root/.nvm/versions/node/v7.1.0/bin/node /root/.nvm/versions/node/v7.1.0/bin/forever start /var/www/server.json
Apparently NVM works in magic by setting up some system paths which aren't setup at the moment the cron jobs run.
I'm trying to configure PM2 to daemonize a simple node.js server on centos. The init.d script executes, and there are no errors, but pm2 list returns an empty list, and the server is not starting.
Originally, I was attempting to start the process as a different user (for those interested, this can be done with modifications to pm2-init.sh). But due to the complication, I decided to first try to get it to run as root.
The strange symptom of the problem is that the dump.pm2 file is being emptied by some process, thus the pm2 resurrect has nothing to do. I set up pm2 to run on reboot as follows
$ pm2 startup centos
$ pm2 start /path/to/my/server.js
$ pm2 save
At this point, dump.pm2 had a large JSON object in it and the server is running. Then, running:
$ reboot
... wait and log back in
$ pm2 list
It was empty again, and so was the dump.pm2!
I'm not sure what process is clearing this file. I've tried reinstalling and re-running the startup script to no avail. Any help is greatly appreciated.
As a temporary workaround, I set the dump file as immutable:
$ chattr +i /path/to/my/dump.pm2
This worked. Although not ideal, it is what I will use until I can determine why the dump.pm2 file is being emptied.