Start and stop a node application using command prompt. - node.js

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.

Related

Nuxt 3 server does not stop when restarting supervisor job

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.

yarn does not start in Jhipster generated project

My newly installed Jhipster project is runned but doesn't display anything on the browser and when I type,'yarn start' at the project teminal
it threw the exceptions/errors as the following image enter image description here...
As your error trace shows that you are trying to use port 9060 which is already in use by any other process.
Solution 1 : run your application on any other port.
Solution 2 : kill the other process which is running on 9060 currently.**
Solution 2 For linux please use following.
// to find process id of the process running on 9060
sudo netstat -anp | grep 9060
//to kill the process
sudo kill -9 <process id>

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.

How to stop a node program while in another command window

I have an ubuntu server running with 2 node websites on it and I keep closing the terminals that start the app so I cannot shut the app down to make changes, I end up restarting the server.
How can I see what node instances are running and then stop them, or how do I stop a node instance through programming and I'll just make a button that kills the node instance. Either way...
If you dont want to use PM2 or systemd you can get the list of all the node.js instances running using
ps -aux | grep node
once you have the list of all nodejs processes you can kill any process using kill command
kill "Process ID goes here"
Use a process manager. PM2 is the most widely recommended.
npm install pm2#latest -g
then
pm2 start app.js
Alternatively you can use something like screen to have multiple terminal sessions running in one window

Starting node app whenever it stops

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

Resources