I have set up a production environment for a nuxt app where the nuxt server gets run through a supervisor job. When the port is not in use this works well. However, if I issue the supervisorctl reload or supervisorctl restart production-frontend commands the previous nuxt server usually fails to exit. This means that when it tries to start the nuxt server again it errors out.
Is there a special trick to get nuxt to work nicely with supervisor or a parameter I can add to the supervisor process so the nuxt server guaranteed shuts down when I restart it? I would rather not have to resort to giving the deploy process the power to kill any previous process that still may be lingering through kill -9 like I now have to do manually.
This is the content of /etc/supervisor/conf.d/production-frontend.conf
[program:production-frontend]
command=/usr/bin/npm start
directory=/var/www/production/frontend
environment=NODE_ENV="production",PORT="3011"
autostart=true
autorestart=true
startretries=10
startsecs=30
user=www-data
stderr_logfile=/var/log/supervisor/production-frontend_error.log
stdout_logfile=/var/log/supervisor/production-frontend_out.log
/var/www/production/frontend contains the .output folder from a npm run build command and a package.json file. npm run start will start the nuxt server with node .output/server/index.mjs.
Whenever the server fails to start the error log shows the following error message:
[nitro] [dev] [uncaughtException] Error: listen EADDRINUSE: address already in use :::3011
Running netstat -pln | grep 3011 shows a still running nuxt server.
As it turns out npm start will run a command
sh -c -- node .output/server/index.mjs
This in turn will start a process
node .output/server/index.mjs
This can be verified with in a shell with the following commands. Note that 191667 is the parent (sh) and 191668 is the nuxt server.
$ netstat -pln | grep 3301
tcp6 0 0 :::3011 :::* LISTEN 191668/node
$ pstree -p 191668
sh(191667)───node(191668)─┬─{node}(191669)
├─{node}(191670)
├─{node}(191671)
├─{node}(191672)
├─{node}(191673)
├─{node}(191674)
├─{node}(191675)
├─{node}(191676)
├─{node}(191677)
└─{node}(191678)
Supervisor will kill the parent process, but not the child process. Instead just run the command directly. In my case running node .output/server.index.mjs is fine. If something is not installed globally you might need to define the entire path to whatever you are trying to run (e.g. ./node_modules/jest/bin/jest this_makes_no_sense_for_supervisor.
Related
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.
I don't have any errors in my node app, but it stops after some requests. Though i have enabled the logs and checked, its showing on different requests.
Is it possible to start the node app whenever it is stopped ?
Currently i am using this to start the node app
To start the app
nohup npm start &
And to find the process id
ps aux | grep node
And to kill certain process
kill -9 PID
I don't want to leave the app stopped. It should be ever running. So, Is it possible to start the app whenever it gets stopped.
Thanks
You can use PM2 to monitor and restart your app for you.
By default if you run your app with PM2 it will auto-restart it if it crashes.
npm install pm2#latest -g
pm2 start index.js
PM2 can also be used to run multiple instances and has commands to stop/restart individual ones.
You can get the process PID using pm2 list as well.
More robust use case:
Start up your app by having PM2 run 'npm start' and also give it a name to refer to it later (it will auto restart if it exits/crashes)
pm2 start npm --name="mySuperApp" -- start
Later you make some changes and want to restart it:
pm2 restart mySuperApp
Eventually you want to stop the app:
pm2 stop mySuperApp
you can use nodemon
installing it with npm install -g nodemon
and running your code like this : nodemon index.js
Finding more documentation here
It will run until your app encounter an error but you can restart it with rs in your console
I have created a node application and run it in 3000 port. Now I need to stop/start/restart this application by using command prompt. How can I do it.
Start:node app.js
Stop:Ctrl + C
Restart:node app.js
Better option try Nodemon. Nodemon is a utility that will monitor for any changes in your source and automatically restart your server.
Install nodemon globally npm install nodemon -g
Run the server nodemon app.js
Assuming you are using linux
If you need to stop some application running on some specific port use this and you know the port but not the process id. find the process id like this
netstat -plten | grep LISTEN | grep 3000
this will output some thing like this
tcp 0 0 :::10060 :::* LISTEN 0 20465 3489/node
where 3489 is the process id.
then do
kill -9 3489
to kill the procees
You can simply press 'Ctrl + C' to stop any process in cmd. Also consider using nodemon , its a fantastic tool which automatically restarts your app whenever you save any new changes to the files.
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
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