Running Node as a Service with Forever [duplicate] - node.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

Related

forever is not working as expected after closing terminal or console

I have installed forever on shared hosting Cpanel for node js application when I run forever start app.js, node js application works on the server.
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.js
But when I close terminal or console then node app stopped working. Any suggestions around it?
Closing the terminal will typically close the application running. Consider using tmux or screen to launch the app or also nohup.
Launching this way should be considered a short-term solution. You probably want to look at how your specific Linux distribution handles startup scripts and services.
like maldina said closing forever will stop your app consider
consider upstart (it runs tasks when the computer is started)
you basiclly create a conf file and place it in your init folder "var/etc/init"
the file content should look like this
#!upstart
description "my-app"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
exec node /somepath/myapp/server.js >> /var/log/myapp.log 2>&1
you can use the following commands to manage your app
sudo start my-app
sudo stop my-app
sudo restart my-app

error: Forever detected script exited with code: 1

I am using forever to restart a nodejs-app in case it crashes. In some situations the app does not get restarted. Forever just displays the message:
error: Forever detected script exited with code: 1
I do not want to solve the issue within the app itself, I just need forever to restart over and over again. Maybe I am missing a paremeter?
The other questions concerning forever not restarting here on SO do not address my specific case.
I think I solved it:
As ist seems both parameters --minUptime and --spinSleepTime have to be set. Otherwise a so called "spinning" script will not be restarted.
So in order to restart a "spinning" script forever needs to have both parameters set like:
forever --minUptime 5000 --spinSleepTime 3000 app.js

Socket server shuts off after 2-3 days

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.

Why does my node app process keep getting stopped when I use forever?

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

How to run node.js app forever when console is closed?

I connect to my remote server via ssh. Then I start my node.js app with Forever. Everything works fine until I close my console window. How to run node.js app FOREVER on my remote server even when I close my connection via ssh? I just want to start an app and shut down my copmputer. My app should be working in the background on my remote server.
You may also want to consider using the upstart utility. It will allow you to start, stop and restart you node application like a service. Upstart can configured to automatically restart your application if it crashes.
Install upstart:
sudo apt-get install upstart
Create a simple script for your application that will look something like:
#!upstart
description "my app"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
exec node /somepath/myapp/app.js >> /var/log/myapp.log 2>&1
Then copy the script file (myapp.conf) to /etc/init and make sure its marked as executable. Your application can then be managed using the following commands:
sudo start myapp
sudo stop myapp
sudo restart myapp
Two answers: One for Windows, one for *nix:
On Windows, you can use the start command to start the process disconnected from your instance of cmd.exe:
start node example.js
On *nix, there are two aspects of this: Disconnecting the process from the console, and making sure it doesn't receive the HUP signal ("hang up"), which most processes (including Node) will respond to by terminating. The former is possibly optional, but the latter is necessary.
Starting disconnected from the console is easy: Usually, you just put an ampersand (&) at the end of the command line:
# Keep reading, don't just grab this and use it
node example.js &
But the above doesn't protect the process from HUP signals. The program may or may not receive HUP when you close the shell (console), depending on a shell option called huponexit. If huponexit is true, the process will receive HUP when the shell exits and will presumably terminate.
huponexit defaults to false on the various Linux variants I've used, and in fact I happily used the above for years until coderjoe and others helped me understand (in a very long comment stream under the answer that may have since been deleted) that I was relying on huponexit being false.
To avoid the possibility that huponexit might be true in your environment, explicitly use nohup. nohup runs the process immune from HUP signals. You use it like this:
nohup node example.js > /dev/null &
or
nohup node example.js > your-desired-filename-or-stream-here &
The redirection is important; if you don't do it, you'll end up with a nohup.out file containing the output from stdout and stderr. (By default, nohup redirects stderr to stdout, and if stdout is outputting to a terminal, it redirects that to nohup.out. nohup also redirects stdin if it's receiving from a terminal, so we don't have to do that. See man nohup or info coreutils 'nohup invocation' for details.)
In general for these things, you want to use a process monitor so that if the process crashes for some reason, the monitor restarts it, but the above does work for simple cases.
I would definitely recommend pm2
npm install -g pm2
To start server: pm2 start [yourServerFile.js]
To stop server: pm2 stop [yourServerFile.js]
Close client and server will run forever....will also restart if app crashes.
Ive been running a node server on Ubuntu for months with zero issues
Always, simple is the best, no need upstart, no need forever, just nohup:
nohup node file.js &
Believe me, I'm running so that for my case!
You could install forever using npm like this:
sudo npm install -g forever
Or as a service:
forever start server.js
Or stop service
forever stop server.js
To list all running processes:
forever list
node expamle.js & for example
In Linux, SSH into your remote server and run
screen
to launch into a new screen.
Finally, type ctrlad to detach the screen session without killing the process.
More info here.
I had similar issue and I think using forever will help to handle crashed and restarts
You can install forever globally:
sudo nom install -g forever
And run this command:
nohup forever server.js &
This should handle all the trouble of closing the terminal, closing ssh session, node crashes and restarts.
If you're running node.js in a production environment, you should consider using PM2, forever.js, or Nodemon.
There is no shortage of articles online comparing the different packages.
This is only a partial answer for Windows. I’ve created a single line Visual Basic Script called app.vbs that will start your node application within a hidden window:
CreateObject("Wscript.Shell").Run "node app.js", 0
To execute it automatically at startup, open the %AppData%\Microsoft\Windows\Start Menu\Programs\Startup\ directory and add a shortcut to the app.vbs file.
More info at: https://keestalkstech.com/2016/07/start-nodejs-app-windowless-windows/
Wow, I just found a very simple solution:
First, start your process (node app)
forever dist/index.js
run: ^Z cmd + z.
Then: bg. Yeah.. bg (background).
And pum.. you are out.
Finish with exitif you are with sshor just close the terminal.
my start.sh file:
#/bin/bash
nohup forever -c php artisan your:command >>storage/logs/yourcommand.log 2>&1 &
There is one important thing only. FIRST COMMAND MUST BE "nohup", second command must be "forever" and "-c" parameter is forever's param, "2>&1 &" area is for "nohup". After running this line then you can logout from your terminal, relogin and run "forever restartall" voilaa... You can restart and you can be sure that if script halts then forever will restart it.
I <3 forever

Resources