I am using laravel and socket IO for real time communication.
I installed node, express, socket IO and forever using npm in the root directory.
command to start file:
sudo forever start socket.js
Scripts run for few days(2-3). After this it stops.
Any suggestion to fix this.
Forever logs all errors by default, you can check the log file in ~/.forever/****.log.
My guess would be that there is an error like:
error: Forever detected script was killed by signal: SIGKILL.
I had a simular issue with forever, using PM2 as nodejs taskmanager solved my problem.
Related
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
On a free-tier Amazon EC2 instance, I set up a simple node.js Hello World app running on express.
If I run npm start, my app runs fine and I can hit it from my browser, and I see the following output:
> myappname#0.0.0 start /home/ec2-user/app
> node ./bin/www
I have installed the forever tool globally. When I run forever start app.js, I see:
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: app/app.js
However, when I check forever list, I see that the process has stopped:
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] 2v0J /usr/local/bin/node app.js 2455 2457 /home/ec2-user/.forever/2v0J.log STOPPED
This is the only message in the log: error: Forever detected script was killed by signal: null
I'm unable to find any other log information. Why does it keep immediately stopping?
EDIT: I tried running it as nohup forever start app.js and got the same problem. I'm running the forever start and the forever list in the same ssh session, one after the other. The app's process seems to stop immediately.
I'm guessing the process stops after you disconnect from ssh?
Try running forever with nohup first.
nohup forever start app.js
When you disconnect from ssh the server kills all your shell's child processes. nohup disconnects a process from its parent shell.
I was able to resolve my problem thanks to this answer on a similar question:
https://stackoverflow.com/a/24914916/1791634
The process kept running when I used forever start ./bin/www instead of passing app.js
It remains to be seen whether this causes any trouble down the road.
For me, I had to use "sudo forever" for it to work.
If you are starting the server after updating the code. Pull the latest code and run
npm install
Now run
forever start app.js
This will fix the issue
I am having hard time understanding what is forever in nodejs.
Can someone explain what is forever in simplest way that i can understand and what is the purpose of it
forever is a node.js package that is used to keep the server alive even when the server crash/stops. When the node server is stopped because of some error, exception, etc. forever automatically restarts it
From the npmjs https://www.npmjs.com/package/forever
A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever)
Forever can be used as
forever start app.js
It provides many useful functions which you can see in the link above.
Directly quoting from http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/
The purpose of Forever is to keep a child process (such as your node.js web server) running continuously and automatically restart it when it exits unexpectedly.
Forever basically allows you to run your nodejs application as a process.
Without forever you might type npm start or node index.js to start your application and it will run in your terminal session.
With forever, you can start it off and still have access to your terminal session/close it.
You can even use the nohup command it simply ignores the hang up signal.
node index.js && nohup -& (to run as a background process - no hiccup)
On a free-tier Amazon EC2 instance, I set up a simple node.js Hello World app running on express.
If I run npm start, my app runs fine and I can hit it from my browser, and I see the following output:
> myappname#0.0.0 start /home/ec2-user/app
> node ./bin/www
I have installed the forever tool globally. When I run forever start app.js, I see:
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: app/app.js
However, when I check forever list, I see that the process has stopped:
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] 2v0J /usr/local/bin/node app.js 2455 2457 /home/ec2-user/.forever/2v0J.log STOPPED
This is the only message in the log: error: Forever detected script was killed by signal: null
I'm unable to find any other log information. Why does it keep immediately stopping?
EDIT: I tried running it as nohup forever start app.js and got the same problem. I'm running the forever start and the forever list in the same ssh session, one after the other. The app's process seems to stop immediately.
I'm guessing the process stops after you disconnect from ssh?
Try running forever with nohup first.
nohup forever start app.js
When you disconnect from ssh the server kills all your shell's child processes. nohup disconnects a process from its parent shell.
I was able to resolve my problem thanks to this answer on a similar question:
https://stackoverflow.com/a/24914916/1791634
The process kept running when I used forever start ./bin/www instead of passing app.js
It remains to be seen whether this causes any trouble down the road.
For me, I had to use "sudo forever" for it to work.
If you are starting the server after updating the code. Pull the latest code and run
npm install
Now run
forever start app.js
This will fix the issue
I am new to linux so I am wondering how can I make service run forever? and automatically restart if it crashes or stops?
I am running Node.js + Socket.io as a chat server.
You have 2 main options for node.js :
Option 1 : node-forever
npm install forever -g
then you run your script by typing : forever start myscript.js
Option 2 : pm2
npm install pm2 -g
then you run your script by typing : pm2 start myscript.js
The main difference is that pm2 has zero downtime, a web interface, console monitor and a built-in load balancer. The web interface itself has proved an invaluable bonus for many of my projects.
I would recommend forever in development mode, and pm2 in production, the reason being that pm2 sometimes keeps the port in use when you kill it, so it's a bit annoying in dev when you restart all the time. Otherwise pm2 has a lot more features and has never disappointed me, I use it all the time.
Take a look at Monit
It's a standard open source app for monitoring processes and restarting them if they fail.
you can use Forever NPM this is easy to use, i was also new but tried this my work gone easy :)
For start you server you can use: monit, forever, upstart or systemd.
For Forever:
Start a process:
forever start example.js
Stop the process
forever stop example.js