Constantly having to kill worker processes to start up my Phoenix app - react-redux

I am currently working on a Phoenix backend with React-Redux frontend application. My task is to develop a new component, but I am starting to get annoyed with having to run this all the time:
$ lsof -i tcp:3000
$ kill -9 PID
because if not it tells me Something is already running on port 3000.
I get this message in terminal even when its just the Chrome browser with docs in the background. I don't want to have to shut down my browser worker processes just to start this app and I have never seen this behavior before.
Where could the problem lie? Keep in mind I did not build this application, and that I am fairly new to the Phoenix framework.
At any given time when I did an lsof -i tcp:3000 these are the processes running:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 13691 username 24u IPv4 0x19aa008389bcc55 0t0 TCP *:hbci (LISTEN)
So in dev.exs it shows port: 4000 but thats the Phoenix backend, there is also a React-Redux frontend running on port 3000 so I don't believe changing the port on the Phoenix backend would help in this case.

I found a temporary solution by running this script before starting up Phoenix:
lsof -i :3000 | awk '$1 == "node" { print $2 }' | uniq | xargs kill -9
lsof - list open files
| awk '$1 == "node" { print $2 } - dollar sign indicates column 1 equals node, if it is grab the PID, unique-ing it so it can kill property and pass the PID and kill it.

Related

how can I use bash to blacklist processes such as sharingd?

I am attempting to use bash to permanently kill the process sharingd.
I have tried using the command "sudo kill -9 (pid of port sharingd is using)", but sharingd just reopens on another port.
lsof -i
sudo kill -9 PID
My expected results should stop sharingd from running, but it just uses a different port each time.
Pardon my inability to display my code as actual code, I am somewhat new to Stack Overflow.

how to stop http-server on a specific port?

I am using http-server in order to load http://localhost:8484 on a specific folder. (For testing purposes)
the os commands I run in my code are:
http-server -p 8484 test/
and after I finish downloading whatever I run:
http-server stop
However, after the test is done, I see that the http-server with port 8484 is still alive!
by running ps aux | grep http
What command should I run in order to stop it?
I am using Mac OSX (El Capitan version)
I write the code in erlang (though I don't think it matters since I am running shell commands from the code).
http-server: https://www.npmjs.com/package/http-server
My code in erlang:
my_test_() ->
Pid = spawn(fun() ->
Info = os:cmd("http-server -p 8484 test/resources"),
io:format(user,"*** Server: ~p~n",[Info])
end),
%%% Do some job %%%
Pid2 = spawn(fun() ->
Info = os:cmd("http-server stop"),
timer:sleep(200),
io:format(user,"*** Server stop: ~p~n",[Info])
end),
timer:sleep(200),
exit(Pid2, kill),
exit(Pid, kill).
Use:
kill -9 {pid}
Also, out of interest, if you want to see all processes running on a specific port, use:
lsof -i :{port}
EDIT: Using kill -9... is a bit harsh, I know, there is probably a more graceful way of doing it, but it does get the job done ;-)
For Windows users use the command prompt (cmd):
Method 1:
Just do Control+c on the same console where the http-server is running
Method 2: Find and Kill the process:
i. Find the process-id which uses the particular port number (say 8484)
netstat -ano | findstr 8484
Result: TCP 0.0.0.0:8484 0.0.0.0:0 LISTENING 21816
ii. Kill the process using the found process-id (say 21816)
taskkill /F /PID 21816
You can use perc - since Node.js http-server is not Erlang process, but Unix process, you need to use the module (or dig it's code to see the implementation ;) ) .
Alternatively, from Erlang os:cmd("kill -9 5607"). (where 5607 is your unix pid);

Mac OS X El Capitan 10.11.2, Cassandra 2.2.3, kill pid not working

Using Mac OS X El Capitan 10.11.2 and started Cassandra ( version 2.2.3) in the terminal by giving "cassandra -f" command.
Cassandra started good and able to connect using cqlsh.
Then quitted cqlsh, so its no more connecting to cassandra # port 9042
I m now trying to kill the running cassandra by doing the below:
ps -ef | grep cassandra
sudo kill -9 PID
But it does not kill the process, still cassandra is running. Not sure how to stop it now.
Just type:
ps aux | grep cassandra
Find the PID (it's always the 5 digits and if it have more than one, choose the lower) and then
kill <PID>
:)
The command ps -ef | grep cassandra should be returning two PIDs, one for cassandra and another for your grep command which also contains the word cassandra. There's a good chance that you're trying to kill the grep command, which has already died. So, make sure that you're killing the correct process.
Also, if all else fails, you can restart.

Unable to stop Webrick launched by "rackup"

I'm developing a Sinatra application and I'm using "rackup" to start Webrick. What should I do to stop it? Now I'm using Ctrl+Z and it seems like it stops. However when I try to start it again it will say that the port is already bound.
I tried it with many ports and each time it started, stopped and then said it was in use when I restarted it again.
How do I solve it?
Ctrl+Z will just "pause" the process, not terminate / kill it.
To truly kill it, find it in the process table and do kill -9 [PID]
like:
ps auxwww | grep ruby
slivu 16244 0.0 0.5 2551140 61220 s020 R+ 1:18AM 0:10.70 ruby app.rb
the second column(16244) is the PID.
The other way is to "catch" the INT signal with Ruby and exit the app explicitly.
in your app:
Signal.trap 'INT' do
Process.kill 9, Process.pid
end
Extending on slivu's reply,
use CTRL+C to kill the process if you are still in the same terminal.
If you are launching it in the background, or want to kill from a different terminal, use
ps aux | grep [r]ackup | awk '{print $2}' | xargs sudo kill -9

How do I close an open port from the terminal on the Mac?

I opened port #5955 from a java class to comunicate from a client. How do i close this port after I am done? and also which command can show me if port open or closed?
Find out the process ID (PID) which is occupying the port number (e.g., 5955) you would like to free
sudo lsof -i :5955
Kill the process which is currently using the port using its PID
sudo kill -9 PID
To find the process try:
sudo lsof -i :portNumber
Kill the process which is currently using the port using its PID
kill PID
and then check to see if the port closed. If not, try:
kill -9 PID
I would only do the following if the previous didnt work
sudo kill -9 PID
Just to be safe. Again depending on how you opened the port, this may not matter.
EDIT
In 09/2022 this helped me for MacOS Monterey M1 Pro Chip:
sudo lsof -t -i tcp:yourPortNumber | sudo xargs kill
In 2018 here is what worked for me using MacOS HighSierra:
sudo lsof -nPi :yourPortNumber
then:
sudo kill -9 yourPIDnumber
very simple find port 5900:
sudo lsof -i :5900
then considering 59553 as PID
sudo kill 59553
However you opened the port, you close it in the same way. For example, if you created a socket, bound it to port 0.0.0.0:5955, and called listen, close that same socket.
You can also just kill the process that has the port open.
If you want to find out what process has a port open, try this:
lsof -i :5955
If you want to know whether a port is open, you can do the same lsof command (if any process has it open, it's open; otherwise, it's not), or you can just try to connect to it, e.g.:
nc localhost 5955
If it returns immediately with no output, the port isn't open.
It may be worth mentioning that, technically speaking, it's not a port that's open, but a host:port combination. For example, if you're plugged into a LAN as 10.0.1.2, you could bind a socket to 127.0.0.1:5955, or 10.0.1.2:5955, without either one affecting the other, or you could bind to 0.0.0.0:5955 to handle both at once. You can see all of your computer's IPv4 and IPv6 addresses with the ifconfig command.
One liner is best
kill -9 $(lsof -i:PORT -t) 2> /dev/null
Example :
On mac, wanted to clear port 9604. Following command worked like a charm
kill -9 $(lsof -i:9604 -t) 2> /dev/null
You can also use this first command to kill a process that owns a particular port:
sudo netstat -ap | grep :<port_number>
For example, say this process holds port 8000 TCP, then running the command:
sudo netstat -ap | grep :8000
will output the line corresponding to the process holding port 8000, for example:
tcp 0 0 *:8000 *:* LISTEN 4683/procHoldingPort
In this case, procHoldingPort is the name of the process that opened the port, 4683 is its pid, and 8000 (note that it is TCP) is the port number it holds (which you wish to close).
Then kill the process, following the above example:
kill 4683
As others mentioned here out, if that doesn't work (you can try using kill with -9 as an argument):
kill -9 4683
Again, in general, it's better to avoid sending SIGKILL (-9) if you can.
Find the process ID using command
lsof -n -i4TCP:8080
After getting the processId
sudo kill -9 processID
Then provide your system password.
I have created a function for this purpose.
function free_port() {
if [ -z $1 ]
then
echo no Port given
else
PORT=$1;
PID=$(sudo lsof -i :$PORT) # store the PID, that is using this port
if [ -z $PID ]
then
echo port: $PORT is already free.
else
sudo kill -9 $PID # kill the process, which frees the port
echo port: $PORT is now free.
fi
fi
}
free_port 80 # you need to change this port number
Copy & pasting this block of code in your terminal should free your desired port. Just remember to change the port number in last line.
I use lsof combined with kill, as mentioned above; but wrote a quick little bash script to automate this process.
With this script, you can simply type killport 3000 from anywhere, and it will kill all processes running on port 3000.
https://github.com/xtrasimplicity/killport
Simple One-liner
There is a way more straightforward command today, than the other ones (without Sudo, packages or multiple lines)
To kill port 8080 simply call:
lsof -ti tcp:8080 | xargs kill
This seem to work for me. Just change your_port_number into the port number you want to stop.
sudo lsof -t -i tcp:your_port_number | xargs kill -9
try below, assuming running port is 8000:
free-port() { kill "$(lsof -t -i :8000)"; }
I found the reference here
When the program that opened the port exits, the port will be closed automatically. If you kill the Java process running this server, that should do it.
First find out the Procees id (pid) which has occupied the required port.(e.g 5434)
ps aux | grep 5434
2.kill that process
kill -9 <pid>

Resources