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.
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
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.
This is probably a simple question but I can't find a clear answer anywhere. I am trying Hello World on node.js. I have a node.js server running on port 8000 of the localhost, turned on via the command line e.g. "node helloworld.js". Helloworld.js runs fine via localhost:8000. Now when I try turn on another server on port 8000 though I get the error "listen EADDRINUSE" because the first server is still running. So how do I turn off the first node server?
Just kill the process by doing ctrl-c...
If you still have the original terminal in which you run the Nodejs server, then simply press ctrl + C can kill the process.
However, if you lost the terminal, then you can open another terminal and run taskkill /F /IM node.exe. (/F to force the kill, /IM to specify which script you want to kill). Note that the command would kill every node server running.
If you no longer have access to your terminal, then go to your task manager on Windows or 'Force Quit' on Mac and end the 'Node.Js...' process.
This is the cleanest way to do it (if no terminal window), in my opinion.
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