How to manually stop kestrel server - asp.net

I am doing this basic tutorial: http://www.asp.net/get-started
It suggests doing some changes, then running dotnet run again. However, Ctrl+C isn't working and the Package Manager Console seems frozen. When I exit VS and/or restart it, I do my changes and dotnet run again.
When I am doing this, I am getting an error (not the same every time), because the server is already running. The question is simple as 1-2-3:
How do I manually stop the kestrel server? (I am running Windows 10).

On OSX Terminal:
sudo lsof -iTCP -sTCP:LISTEN -P | grep :5000
sudo kill -9 1872

I ran this on powershell to kill all instances of the kestral server
Get-Process -Name *dotnet* | Stop-Process

For windows users
run netstat -ano -p TCP | find /I "listening" | find /I "port number"
then run taskkill /F /PID process ID on Command Prompt.
Here is an example
C:\Users\Kiran>netstat -ano -p TCP | find /I "listening" | find /I "2492"
TCP 127.0.0.1:2492 0.0.0.0:0 LISTENING 820
C:\Users\Kiran>taskkill /F /PID 820
SUCCESS: The process with PID 820 has been terminated.
Here netstat -ano -p TCP | find /I "listening" | find /I "2492" finds the process running TCP protocol listening on port 2492. And taskkill /F /PID 820 command to forcefully terminate the process with id 820

If anything else fails you can simply close it from the task manager. The name of the process would be dotnet. If you have Visual Studio open (in my case at least), there may be at least one other dotnet process running. Could not distiguish between them using only the task manager...
PS: When launching Kestrel with dotnet run from the PMC, in the console you get this:
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
But it didn't work to stop it with Ctrl+C

The line below works for me.
killall -9 dotnet

Stumbled upon it today. The answer above looks cool but netstat is slow. A simpler way to kill kestrel would be to use the PowerShell commandlet Get-NetTCPConnection.
To kill kestrel listening on Port 5000, the command would be
$pId = (Get-NetTCPConnection -LocalPort 5000).OwningProcess[0]
kill -Id $pId

Save the following in a .cmd file and click on the file:
Taskkill.exe /F /IM dotnet.exe /T

Ctrl + Break also does the trick.

Related

Jupyter notebook: The port is already in use, trying another port?

I can join the port sucessfully by
ssh -N -L 8888:127.0.0.1:8888 server_ip
But if I just join the server, and then use jupyter notebook, I can't open the port sucessfully.
ssh server_ip
jupyter notebook --port=8888
Error:
The port 8888 is already in use, trying another port.
What is the reason and how to solve it?
You can check which application is currently running on port 8888 by following commands on Linux machine. If in case it happens to be an existing jupyter, you may try killing that process and start a new one.
Check current process running in 8888
lsof -i:8888
To kill the current process.
kill $(lsof -t -i:8888)
To forcefully kill the current process
kill -9 $(lsof -t -i:8888)
If for some reason you wish not to kill the current process, you may try launching jupyter on a different port.
jupyter notebook --port=8889
Sounds like you have some other process on port 8888.
You can either:
1) Kill the process on port 8888 and then launch jupyter-notebook on port 8888
2) Launch jupyter notebook on a different port and then use ssh to connect to the new port

Grunt Task - freeing up the port

I setup this.
http://www.zell-weekeat.com/grunt-sass-with-susy/
It works fine for one run then, if I exit out of the sass watch I can't free up the port.
Fatal error: Port 35729 is already in use by another process.
Then
sudo lsof -i :35729
gives
\COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
grunt 26305 author 12u IPv6 7064373 0t0 TCP *:35729 (LISTEN)
How do I get this port to free up
Just for organization sake, as mentioned in comments:
You should stop a process with Ctrl+C in the terminal.
Ctrl+Z will keep it running.
If you need to kill it, find the PID with sudo lsof -i :35729 and then kill -9 PID.
Just sudo netstat -tulpn | grep -i : <port>. kill it with kill -9 PID

Dockerized nginx is not starting

I have tried following some tutorials and documentation on dockerizing my web server, but I am having trouble getting the service to run via the docker run command.
This is my Dockerfile:
FROM ubuntu:trusty
#Update and install stuff
RUN apt-get update
RUN apt-get install -y python-software-properties aptitude screen htop nano nmap nginx
#Add files
ADD src/main/resources/ /usr/share/nginx/html
EXPOSE 80
CMD service nginx start
I create my image:
docker build -t myImage .
And when I run it:
docker run -p 81:80 myImage
it seems to just stop:
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
90e54a254efa pms-gui:latest /bin/sh -c service n 3 seconds ago Exit 0 prickly_bohr
I would expect this to be running with port 81->80 but it is not. Running
docker start 90e
does not seem to do anything.
I also tried entering it directly
docker run -t -i -p 81:80 myImage /bin/bash
and from here I can start the service
service nginx start
and from another tab I can see it is working as intended (also in my browser):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
408237a5e10b myImage:latest /bin/bash 12 seconds ago Up 11 seconds 0.0.0.0:81->80/tcp mad_turing
So I assume it is something I am doing wrong with my Dockerfile? Could anyone help me out with this, I am quite new to Docker. Thank you!
SOLUTION: Based on the answer from Ivant I found another way to start nginx in the foreground. My Dockerfile CMD now looks like:
CMD /usr/sbin/nginx -g "daemon off;"
As of now, the official nginx image uses this to run nginx (see the Dockerfile):
CMD ["nginx", "-g", "daemon off;"]
In my case, this was enough to get it to start properly. There are tutorials online suggesting more awkward ways of accomplishing this but the above seems quite clean.
Docker container runs as long as the command you specify with CMD, ENTRTYPOINT or through the command line is running. In your case the service command finishes right away and the whole container is shut down.
One way to fix this is to start nginx directly from the command line (make sure you don't run it as a daemon).
Another option is to create a small script which starts the service and then sleeps forever. Something like:
#!/bin/bash
service nginx start
while true; do sleep 1d; done
and run this instead of directly running the service command.
A third option would be to use something like runit or similar program, instead of the normal service.
Using docker-compose:
To follow the recommended solution, add to docker-compose.yml:
command: nginx -g "daemon off"
I also found I could simply add to nginx.conf:
daemon off;
...and continue to use in docker-compose.yml:
command: service nginx start
...although it would make the config file less portable outside docker.
Docker as a very nice index of offical and user images. When you want to do something, chances are someone already did it ;)
Just search for 'nginx' on index.docker.io, you will see, there is an official nginx image: https://registry.hub.docker.com/_/nginx/
There you have a full guide to help you start your webserver.
Feel free to take a look at other users nginx image to see variants :)
The idea is to start nginx in foreground mode.
If you run "service nginx start", it is a parent process which will start a child process of nginx. If you run "service nginx start" as CMD in a container, the Process ID 1 for the container will be "service nginx start" or ServiceManager (SystemD), while actual nginx would be running as a child process.
If you run "service nginx start", and then "ps -ef", you will get output as below. I have run it my host OS.
root#ip-172-31-85-74:/home/ubuntu# service nginx start
root#ip-172-31-85-74:/home/ubuntu#
root#ip-172-31-85-74:/home/ubuntu# ps -ef | grep nginx
root 18593 1 0 12:27 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 18595 18593 0 12:27 ? 00:00:00 nginx: worker process
root 18599 17918 0 12:27 pts/0 00:00:00 grep --color=auto nginx
So, here the process ID 18593 is the child process which has parent process 1.
Container exits when their Process ID 1 exits. And in case of CMD "service nginx start", the PID 1 is the process manager, may be SystemD. It starts nginx as a child process, and exits itself, hence the container exits.
Similarly, if you run a shell script (for eg : start.sh) in CMD, as soon as the script ends, the container will exit. Even though the script starts some services (eg - nginx) in its execution, as soon as the script ends, the container will exit, because the PID 1 will be of the shell script. The parent process will be "./start.sh", and the services started by script will be child processes. In case you want to use a shell script in CMD, and want the container to run indefinitely, you need a command at last of the script which doesn't let it end. Something as shown below:
#!/bin/bash
service nginx start
while true; do sleep 1d; done

How to gracefully reload a spawn-fcgi script for nginx

My stack is nginx that runs python web.py fast-cgi scripts using spawn-fcgi. I am using runit to keep the process alive as a Daemon. I am using unix sockets fior the spawed-fcgi.
The below is my runit script called myserver in /etc/sv/myserver with the run file in /etc/sv/myserver/run.
exec spawn-fcgi -n -d /home/ubuntu/Servers/rtbTest/ -s /tmp/nginx9002.socket -u www-data -f /home/ubuntu/Servers/rtbTest/index.py >> /var/log/mylog.sys.log 2>&1
I need to push changes to the sripts to the production servers. I use paramiko to ssh into the box and update the index.py script.
My question is this, how do I gracefully reload the index.py using best practice to update to the new code.
Do I use:
sudo /etc/init.d/nginx reload
Do I restart the the runit script:
sudo sv start myserver
Or do I use both:
sudo /etc/init.d/nginx reload
sudo sv start myserver
Or none of the above?
Basically you have to re-start the process that's loaded your Python script. This is spawn-cgi and not nginx itself. nginx only communicates with spawn-cgi via the Unix socket and will happily re-connect if the connection is lost due to a restart of the spawn-cgi process.
Therefore I'd suggest a simple sudo sv restart myserver. No need to re-start/re-load nginx itself.

How to shutdown supervisor process correctly/completely?

I am using supervisor to launch and manage a nginx process. So far this works perfectly. The problem I am having is shutting down the instance.
I have tried using "supervisorctl -c shutdown [all]" and this shuts down the daemon and in the supervisorctl interactive console it says nginx is stopped. However, if I do a ps -A | grep nginx command it still appears in the list.
My config for the nginx instance is as follows:
[program:nginx]
command=./bin/nginx
-p /home/me/sites/project.domain.com/
-c project/etc/nginx.conf
directory=/home/me/sites/project.domain.com
autostart=true
autorestart=true
redirect_stderr=true
exitcodes=0
stopsignal=TERM
Any suggestion why nginx could not be shutting down?
Have you made sure you are not starting up nginx in daemonized mode? It is important you start all your child-processes of supervisor in non-daemonized mode. I currently don't have nginx boot options at hand, but this might give you a start in the right direction.

Resources