Forever.js doesn't restart my Node.js / Express app - javascript

I'm using forever.js to make sure my Node.js / Express app runs without a break. However, sometimes it crashes and forever doesn't restart it.
I'm using
forever start app.js
to start the app
and it starts fine, works, and then at some point crashes and when I do
forever list
it doesn't list anything, so it simply doesn't restart...
I also tried running it with a log file, using
forever start -l foreverlog.txt app.js
and the log file is fine, but it doesn't show any info about the end of the process - e.g. the crash or error report, which I usually have if I run the app.js from my console directly.
Do you know how I could make forever restart the app or at least get the errors into the log?
Thank you!

Forever splits the log
-l LOGFILE Logs the forever output to LOGFILE
-o OUTFILE Logs stdout from child script to OUTFILE
-e ERRFILE Logs stderr from child script to ERRFILE
Even if you didn't specify the -e file it should be somewhere. Users/{UserName}/.forever on windows.
You should check the ERRFILE. Maybe that will bring some light to why the process failed.

You need to use spinSleepTime:
forever --minUptime 1000 --spinSleepTime 1000 -w -l *.log -e *.log --port 1234 app.js
--minUptime not set. Defaulting to: 1000ms
--spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms

Try killing the process manually by searching for the process with
ps axl | grep node
then
kill 24597
replace 24597 with your process number. Then reboot forever again.
http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/

Related

Node.js server is not stopping on forever stop 0

My case is this:
I am running a node.js server at domain.com:54321
The command I use to start the server is:
forever start -l forever.log -a -o out.log -e err.log index.js
However, there are some cases where our code gets into a high demanding function, causing the script to work very slow or unresponsive. We are trying to optimize it.
In that case, I stop the server, and start a new one if needed, lets say domain.com:67890
forever stop 0
But if I want to start again the recently stopped node.js server domain.com:54321 (or restart it instead of stopping it) with this, I was expecting the processes to stop and run as fresh and fast again.
The thing that if I start it again, or restart it, it continues to go high on resources. I found out that I need to leave it for a couple of hours to start it again.
My question is, are there any other commands that will make sure that every instance, resource of that server is stopped, so I can start using it again immediately?
Thank you
You can stop a Forever daemon using the id of forever running using four different ways:
index
id
uid
pid
I can see one of the answer to covering the stopping a forever running daemon using pid, I can add for other methods.
List running Forever instances:
$ forever list
info: Forever processes running
|data: | index | uid | command | script |forever pid|id | logfile |uptime |
|------|-------|-----|------------------|-------------|-----------|-----|------------------------|--------------|
|data: | [0] |f4Kt |/usr/bin/nodejs | src/index.js|2131 | 2146|/root/.forever/f4Kt.log | 0:0:0:11.485 |
$ forever stop 0 index
$ forever stop 2146 id
$ forever stop --uid f4Kt uid
$ forever stop --pidFile 2131 pid
To make sure to stop the process you can use the pid option:
forever start -l forever.log -a -o out.log -e err.log --pidFile ~/app_pid index.js
forever stop --pidFile ~/app_pid
Run forever list then forever stop **id**
Here is a sample output
$ forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] 9Xzw ng serve --host 0.0.0.0 --port 300 1111 2312 /home/ec2-user/.forever/9Xzw.log 1:3:20:50.412
data: [1] wOj1 npm run-script app-start-dev 29500 24978 /home/ec2-user/.forever/wOj1.log 0:0:5:3.433
Check your process id and kill
forever stop 0 OR forever stop 1 OR forever stop 2
Here 0, 1, 2 index of process, data:[0], data:[1]
May be kill all node processes forcefully if that is what you want.
follow this answer to kill all node process : https://stackoverflow.com/a/14790921/5055850
check this, if your port are 8000 , it will stop forever
sudo kill -9 `sudo lsof -t -i:8000`
I had the same problem. For me this worked:
forever stopall.

pm2 stopped running my program. Why and when?

I had a node process running with pm2. It showed up in the result of pm2 list. Now I see the process isn't running anymore, and pm2 list is empty.
Are there any pm2 logs which show me when and why it stopped running my node process?
Well first of all you have the pm2 logs command that will show you logs, but pm2 is running by the user that ran the pm2 start commant. Meanning that if you were in sudo mode and ran pm2 start you will not see the application list from a regular user.
If this is not the issue, you should know that if you restart your machine, the list deletes. if you want to keep the list after restart, you need to run pm2 save after you finish starting your applications/json configuration file.

keep getting "? Something is already running on port 3000" when I do npm start on react app

I keep on getting "? Something is already running on port 3000" message in my terminal when I start up my react server when there is absolutely nothing running on my port 3000
What I have tried to solve with:
Restart the macOS.
checking my "http://localhost:3000" on chrome browser. (Nothing: This site can’t be reached)
Go to chrome://serviceworker-internals and chrome://appcache-internals, search for localhost:3000 (Nothing found)
I also tried almost every command lines I found on Google regarding this issue
I also created another express.js app, and it was successfully launch on port 3000 while React said there is something running on 3000. React on my computer just keeping thinking there is something running on port 3000.
If you are a windows user you can try using
npx kill-port 3000
in your console. I was having the same problem and it worked for me.
Not sure about MAC.
Open cmd and write this
netstat -a -o -n
You will get list of active connections then find 3000 by hitting
Cntrl + f
Copy the PID of that port and hit this command
taskkill /F /PID PID_of_port
Edit
This guide is for windows.
Kill Node.Js process from Task Manager.
Step 1: Open Task Manager by clicking ctrl+shift+delete
Step 2: Open Prcesses tab
Step 3: Search for Node.JS process and right click on that then click on End Task
Step 4: Now you can start again.
As #khurram khan suggested terminating the process may be the best option for you, This work for me on linux:
$ lsof -i tcp:3000
$ kill -9 PID
the first command should give you the PID number to enter in the second command as PID.
I had this problem on Mac and I solved by running:
npx kill-port 3000
I had stuck with this one for few hours, and the end I had found the solution
There was incorrect mapping to the local host in the hosts file and didn't had any relation to the port taken something like
10.2.224.130 localhost
Just Change it back to
127.0.0.1 localhost
Host file locations
https://www.howtogeek.com/howto/27350/beginner-geek-how-to-edit-your-hosts-file/
In case anyone comes across this and the above solutions didn't help, make sure your /etc/hosts has:
127.0.0.1 localhost
For some reason this was wiped from my hosts file and CRA's dependency for checking ports (detect-port-alt) checks localhost:[PORT] to see if it's available. If it errors out, you'll always get the "Something is already running..." error when checking a specific port but not when using a random port (since that's picked by the dependency).
kill node.js from the background processes. that would soleve the probelem. alterntively, you could restart your system
for window use this in package.json
"start": "set PORT=3006 && react-scripts start"
for Linux and MacOS
"start": "PORT=3006 react-scripts start"
This worked for me on windows pc. This one is for those are not seeing the port when you run this command netstat -a -o -n on your command prompt.
Open your command prompt in administrator mode and run this command
net stop winnat
you'll get this response:
The Windows NAT Driver service was stopped successfully.
Them you run this next:
net start winnat
then you will get this response:
The Windows NAT Driver service was started successfully.
once you do that. Start the react server and it would work. Same too if your backend server doesn't run on 3000
It's very simple. You can fix it in 2 easy steps.
Check your environment variables if there is a key/entry with name "PORT".
If found delete that entry or rename it to something else.
It turns out that some other program is using that variable. Usually when you start react-scripts it will look for an environment variable with that title PORT.
Date: Sat 07, October 2020
Windows: Microsoft Windows 10 Pro Build 19041
Node: 12.16.1
NPM: 6.14.8
Something similar was happening to me on my Windows machine. Tried a lot of things suggested here on StackOverflow and other places.
In my case, I was following a video course that was suggesting adding --script-version 1.1.5 (as illustrated below) when creating a new React app.
create-react-app app-name --scripts-version 1.1.5
Here's the package.json scripts versions:
React: 17.0.1
React DOM: 17.0.1
React Scripts: 1.1.5
After running the command npm start and typing y to try to use a different port, the terminal will stay stucked until it was canceled.
Then I created a new React app without --scripts-version 1.1.5 and after trying to running it, it was still running into Something is already running on port 3000 but after typing y, the app will run with no problem on a different port.
Here's the package.json scripts versions (when it was running successfully):
React: 17.0.1
React-Dom: 17.0.1
React-Scripts: 4.0.0
Starting with the reason you do not see anything on localhost:3000, is because there must be a proper webapp or a website/server running on that port, but the port:3000 is currently running some process, just not of a server/site/app.
This out of the way, depending on your type of OS, it is quite easy to check if there is in-fact a process on port:3000 or not. For MacOS, opening a terminal and running sudo lsof -i ':3000' will list the current process on port 3000.
Note that we need to run command as sudo or root
Once you see what process is running, note the PID and run kill -9 {PID}, which should kill the process with PID (i.e distruptive process on port:3000). Now doing a npm start should get your app built, compiled and served on localhost:3000
Mostly what might've happened is that you had a npm start running which you either interrupted, or closed in the foreground. Doing a fg in your terminal will open any background tasks, so if this opens a npm start instance, it was this process stopping your flow. You can then properly end the session making sure all processes on port:3000 are killed.
Happy coding
If you are on linux you can try
pkill -f node
to terminate the processs
The error occurs when a task is left running on the port properly killing it.
this error can occur on both ports. Your react app and your node server.
To solve this you can run the following commands.
netstat -anp tcp | grep 3000
This command in the terminal will give you the list of activities on port 3000
npx kill-port 3000
This command will kill all the running servers on port 3000.
Now if you have the same problem for your Node server. You can follow the same steps.
netstat -anp tcp | grep 5000
Then
npx kill-port 5000
You don't need the first command. it's just to list out the running activities on the port.
just change the port number for any other port number.
This is the output you get after running the commands.
I am using Git bash on windows.
For me, this works every time (macOS): sudo kill -9 $(sudo lsof -t -i:3000)
Simple and Easy solution
close your current terminal and open a new one.
If you are running it in VS Code just create a new terminal and delete the old one.
On React - you can run an already created React single-page application (SPA) by
npm start command.
That may start your locally hosting development server and runs your app at:
http://localhost:3000/ which is equivalent to: 127.0.0.1:3000 address
127.0.0.1 is the default localhost IP number while the default port number set by
create-react-app package is 3000.
When getting: “Something is already running on port 3000" failure error message you may think that the port captured by another process running on your machine but you’ll find that it is captured permanently as if it runs on 0.0.0.0:3000 address
Solution:
In your project libraries created by create-react-app script navigate to:
node_modules/react-scripts/scripts/start.js
While running npm start command - the start.js script is being called and executed
There at start.js file in you editor find the above line:
const HOST = process.env.HOST || '0.0.0.0';
and change it to:
const HOST = process.env.HOST || '127.0.0.1';
save and run your web app again at: http://localhost:3000/ or http://127.0.0.1:3000

Node.js Forever.js uptime stopped

I'm running a node script on my EC2 instance. I 'm using forever.js to run it because I'd like to run it even when I disconnect from my SSH connection.
I run:
forever start app.js
and then list running processes using:
forever list
and for uptime is says STOPPED no matter what I do.
ubuntu#ip-xx-xxx-xxx-xxx:~$ forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] 79f1 /usr/local/bin/node app.js 17099 17100 /home/ubuntu/.forever/79f1.log STOPPED
You could just run forever logs yourfile.js to see logs. If you need to be sure that you are seen the right logs.
I checked the log file mentioned (duh!) /home/ubuntu/.forever/79f1.log and that had the error.
Turns out, it was a simple path error in my app.js script. I was using a file which wasn't in the same directory from which I ran forever start app.js. So it actually had nothing to do with forever.js, but was a simple path error where node couldn't find a file. Thank goodness for log files!

Forever and node.js, stopping one forever process stops all processes [duplicate]

I am deploying two node.js apps on the aws, the two apps are in the paths shown as
/home/ubuntu/nodes/app1/app.js
/home/ubuntu/nodes/app2/app.js
respectively
to run the node.js apps in the background, I used forever to start two apps, so like
$ sudo forever start /home/ubuntu/nodes/app1/app.js
$ sudo forever start /home/ubuntu/nodes/app2/app.js
so forever works well by running the two node.js apps in the background process.
However, when I tried to stop one process with forever command like this.
$ sudo forever stop /home/ubuntu/nodes/app1/app.js
unexpectedly, both node.js process are closed with info like this
info: Forever stopped process:
data: uid command script forever pid logfile uptime
[0] r2pZ /usr/bin/nodejs app.js 24852 24854 /root/.forever/r2pZ.log 0:0:1:14.775
[1] 9f2h /usr/bin/nodejs app.js 24870 24872 /root/.forever/9f2h.log 0:0:0:58.733
I assume it is because two node.js process has the same name - app.js, how to avoid this by close only one process
You can use an uid (see here):
$ sudo forever --uid "app1" start app.js
$ sudo forever --uid "app2" start app.js
And to stop:
$ sudo forever stop app1
Update
The --uid option is deprecated.
Now you can use the --pidFile option. Example:
forever start --pidFile /some/path/app1.pid app.js
forever start --pidFile /some/path/app2.pid app.js
And to stop:
forever stop --pidFile /some/path/app1.pid
You can kill only one process using the index of the process shown in the forever list command. For example, if you type forever stop 1, only the process with the index 1 will be killed

Categories