PM2 Server don't start - node.js

When I try to start the pm2 server via SSH connection pm2 command never executes and just stops after PM2 Done, but the server never runs.
https://i.stack.imgur.com/UYbCi.png
What can be a problem in this case? I tried a lot of things but nothing works for me.
I tried other solutions like foreverjs but when I close ssh terminal the server is dying. I think only PM2 can help me to run a server in the background after close ssh terminal in Cpanel on Bluehost.
Update 1: If I break the start command when it gets noting after PM2 Done
https://i.stack.imgur.com/l0LH6.png
If I do not break the start command it just will continue maybe forever and the server never starts
https://i.stack.imgur.com/gNos9.png
Command to start the server: pm2 start main.js

Related

How to keep alive node server permanently?

I have tried so many ways to keeping node server alive on Linux environment but nothing has worked. Sometime the server runs only 4-5 hours and sometime it runs 10-12 hours and after that server goes shut down automatically.
I have tried forever start, pm2, nodemon but nothing has worked.
I have also tried shell script with forever start for running it but that also not worked.
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:
$ pm2 startup systemd
Run the command that was generated (similar to the highlighted output above, but with your username instead of sammy) to set PM2 up to start on boot (use the command from your own output):
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy
check here for details https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
You can increase the size of memory restar- check this: pm2 process crashed on server. it gives an error
Try using process manager for making the application run all the time. Here is the link for Pm2 . It will restart you application once it crashes also automatically
Use a NPM package called nodemon
npm install -g nodemon
nodemon index.js
If the application fails or crashes for any reason it will restart
Read more at
https://www.npmjs.com/package/nodemon

Getting error when trying to launch node.js app with PM2 Cluster

in an Ubuntu Server, I am unable to run the node.js app in cluster mode using PM2.
The command I use is :
PM2 start server.js --name Server -i max
When I list the PM2 processes, I can see the Server has Error status.
I have tried looking into the log file generated by PM2 but it's empty.
I am however able to run the same server.js without the cluster mode using :
PM2 start server.js --name Server
doing PM2 Kill and starting all the services again was the solution to above issue.
You could also have used pm2 restart Server to restart it
If you use pm2 kill you will just kill all processes, to clean up afterwards i would recommend to use pm2 flush so all logfiles will be reset
I have gone through this same kind of situations but in my case pm2 is showing error status cause of error in my code.
use the below command
pm2 logs
pm2 logs command helped me by showing some hints to check where exactly the error is occured.
if everything works fine then pm2 list will show you the status online.
you can check the ports running by pm2(not only pm2 but all the process) using below command
sudo netstat -tulpn

Crtl+C not killing server

I'm running Windows 10 and when I try to kill the process using Crtl+C it doesn't work and I have to run :
netstat -ano | findstr :yourPortNumber
taskkill //PID typeyourPIDhere //F
That stops the process and when I run nodemon server restarts fine, but if I have mongod running then nodemon throws port already in use and it crashes.
Edit 1:
I am now able to use nodemon by starting my server through the nodemon command on my git bash (nodemon app.js). This has solved most of my issues, however I still can't close my server through the ctrl+c shortcut, I have to manually kill the server task every time I want to close it or if I make changes to my main file.

keep server alive after closing command prompt (with forever or forever-monitor)

I'm using Amazon WS to test some rudimentary nodejs server. The problem I'm having is that when I close the putty command prompt on my PC, that I can not reach the server anymore with a browser.
Ive read about forever and forever-monitor. I'n not sure why the script must be restarted constantly, but ok let's assume it must.
I'm using both
forever "/home/ec2-user/myApp.js"
and
node "/home/ec2-user/foreverMonitor.js"
(The latter has the myApp.js reference in the foreverMonitor.js file. Similar to Where place forever-monitor code?.)
Both do start the server, but when I close putty, both also let the server die.
What am I missing here?
------------------------------------- update -------------------------------------
I guess I can also skip foreverMonitor (not verified yet)
nohup forever "/home/ec2-user/myApp.js" &
forever stop "/home/ec2-user/myApp.js"
------------------------------------- update -------------------------------------
working, now using this
nohup forever "/home/ec2-user/foreverMonitor.js" &
forever stop "/home/ec2-user/foreverMonitor.js"
I'm not totally familiar with AWS, but it seems that you probably need to run nohup. The trailing ampersand should give you control of the terminal again immediately after executing the command.
$ nohup forever "/home/ec2-user/myApp.js" &
$ nohup node "/home/ec2-user/foreverMonitor.js" &
See this answer for more details on nohup and the trailing ampersand: https://stackoverflow.com/a/15595391/498624
Have a look at PM2 https://github.com/Unitech/pm2
After using forever successfully, I switched to PM2.
forever works fine but I found PM2 was a better fit to my mental model. PM2 also has a very neat (and repidly evolving) Web interface where you can monitor and control node instances. As a bonus you can also run non-node tasks under PM2

Cannot get my Upstart script to run Node.js and Forever when server restarts

I've been setting up my server recently and today I had to restart it... then I realised all of my Node apps I had running weren't running anymore. I'm using Node Forever module to keep the apps running, but then I realised I still need to have them starting when my server restarts or shut downs and powers up again.
I have been researching the best way to do this, but what I'm trying just doesn't seem to work. I've created an Upstart script in my /etc/init/ folder on my Ubuntu Server 10.04LTS remote server and tried restarting and it doesn't seem to do anything. Nothing is getting listed when I run forever list.
Here is my current Upstart script I was trying out today:
#/etc/init/myapp.conf
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
script
exec sudo /usr/local/bin/node /var/www/myapp/myapp.forever.js
end script
I use Forever in a Node script as I find it easier to configure it how I want. It's confirmed that the script runs just fine if I do this outside the script, there is just something wrong with the Upstart script itself. It seems to have the same permissions as all the other Upstart scripts in /etc/init/ folder.
As an additional note, I have gone through almost all the answers I could find here on StackOverflow, and that it how I got together the script that I have at present.
UPDATE:
With Tom's answer, I have now tried:
#/etc/init/myapp.conf
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
exec sudo /usr/local/bin/node /var/www/myapp/myapp.forever.js
But it's still not working.
So I don't know why this isn't running when I restart my server. Please help!
This is not a very happy setup. The way upstart works is that it starts your process running it using the process id for the start command. Forever JS works similarly, it is probably inspired by Upstart.
When you try to run forever.js with upstart, the forever process you create in your upstart script exits immediately after starting. Upstart counts on having the process continue to run.
When I tried to run forever using upstart, I wound up with five different forever process running because upstart thought it had failed to start forever, and it retried five times.
Did you try doing it without the start script lines?
description "my server"
author "name"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
#respawn if you were not using forever
exec sudo /usr/local/bin/node myapp.forever.js
Source: http://caolanmcmahon.com/posts/deploying_node_js_with_upstart
I've opted to use an #reboot statement in the user's crontab file, which will execute forever on server restarts.
#reboot forever start app.js
Additional Reading - http://www.cyberciti.biz/faq/linux-execute-cron-job-after-system-reboot/

Resources