Stopping node.js process on Mac - macos

I have a node.js application that uses some socket ports. Sometimes, when I exit the application with Ctrl + C, some kind of node process is left running, still allocating the socket ports. Thus, I cannot restart my application, since it fails to open those ports. When I look the situation with ps, I get the following response:
$ ps
PID TTY TIME CMD
40454 ttys000 0:00.11 -bash
41643 ttys001 0:00.00 (node)
41741 ttys001 0:00.00 (node)
Trying kill -9 41643 doesn't kill the process. Is it a some kind of unkillable zombie? How can I get rid of those (node)-things blocking my tcp ports?

I'm not a MAC user, but here is what I use to kill all the available node processes (under linux):
sudo killall -9 node

On macOS, it's simply:
sudo killall -9 node
For a lot of the times, sudo is overkill, but in your case, it looks like you might want to try sudo.

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.

Kill -9 -1 behaviour on MacOS

Our teachers told us to experiment with the terminal and kill -9 -1
To my understanding, on UNIX based OS, the first process charged is Init with PID -1 from which the other processes will spawn. I assumed that you couldn't kill it as it is charged in a secured part of memory.
On a VM running LinuxMint, the command would would cause the session to close itself. On MacOS, it would close/crahs(?) all applications.
On some other people laptops running different distribution of Linux the command would be denied which was the behaviour I would have expected in any OS.
So I am confused by the behaviour of the command.
What should be the normal result? Or is it bound to each OS implementation?
Thanks.
The general behaviour, from Kill MAN page:
If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init)
So, "kill -9 -1" will kill all processes it can.

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>

How to kill a process in MacOS?

I tried kill -9 698 but the process did not die.
$ ps -ef | grep chromium
502 698 811 0 0:01.24 ?? 0:07.28 /Users/lucius/chromium/src/xcodebuild/Debug/Chromium.app/Contents/MacOS/Chromium
502 854 732 0 0:00.00 ttys001 0:00.00 grep chromium
$ kill -9 698
$ ps -ef | grep chromium
502 698 811 0 0:01.24 ?? 0:07.28 /Users/lucius/chromium/src/xcodebuild/Debug/Chromium.app/Contents/MacOS/Chromium
502 854 732 0 0:00.00 ttys001 0:00.00 grep chromium
If you're trying to kill -9 it, you have the correct PID, and nothing happens, then you don't have permissions to kill the process.
Solution:
$ sudo kill -9 PID
Okay, sure enough Mac OS/X does give an error message for this case:
$ kill -9 196
-bash: kill: (196) - Operation not permitted
So, if you're not getting an error message, you somehow aren't getting the right PID.
Some cases you might want to kill all the process running in a specific port. For example, if I am running a node app on 3000 port and I want to kill that and start a new one; then I found this command useful.
Find the process IDs running on TCP port 3000 and kill it
kill -9 `lsof -i TCP:3000 | awk '/LISTEN/{print $2}'`
If you know the process name you can use:
killall Dock
If you don't you can open Activity Monitor and find it.
Do you can list the process using a port with command lsof, for example:
lsof -i tcp:PORT_NUMBER_HERE
Replace the word PORT_NUMBER_HERE to the port number that you are using, then a the process running on the port will be listed. Then you have just to kill the process ID founded like this:
kill PID_NUMBER
Where PID_NUMBER is the process ID running on the port.
If kill -9 isn't working, then neither will killall (or even killall -9 which would be more "intense"). Apparently the chromium process is stuck in a non-interruptible system call (i.e., in the kernel, not in userland) -- didn't think MacOSX had any of those left, but I guess there's always one more:-(. If that process has a controlling terminal you can probably background it and kill it while backgrounded; otherwise (or if the intense killing doesn't work even once the process is bakcgrounded) I'm out of ideas and I'm thinking you might have to reboot:-(.
I just now searched for this as I'm in a similar situation, and instead of kill -9 698 I tried sudo kill 428 where 428 was the pid of the process I'm trying to kill. It worked cleanly for me, in the absence of the hyphen '-' character. I hope it helps!
Given the path to your program, I assume you're currently running this under Xcode, and are probably at a debug breakpoint. Processes cannot be killed in this state because of the underlying implementation of breakpoints.
The first step would be to go to your Xcode process and stop debugging. If for some strange reason you have lost access to Xcode (perhaps Xcode has lost access to its gdb sub-process), then the solution is to kill the gdb process. More generally, the solution here is to kill the parent process. In your case this is PID 811 (the third column).
There is no need to use -9 in this case.
I have experienced that if kill -9 PID doesn't work and you own the process, you can use kill -s kill PID which is kind of surprising as the man page says you can kill -signal_number PID.
in the spotlight, search for Activity Monitor. You can force fully remove any application from here.
I recently faced similar issue where the atom editor will not close. Neither was responding. Kill / kill -9 / force exit from Activity Monitor - didn't work. Finally had to restart my mac to close the app.

Mac OS X terminal killall won't kill running process

I have an instance of lighttpd running. When I do "ps -axc" the process is listed as
"614 ?? 0:00.15 lighttpd"
But when I do "killall lighttpd" I get
No matching processes belonging to you were found
I'm on Mac OS X 10.5.6. Is there something I'm missing?
As per the other response, if it's not your process, prepend sudo if you're an administrator. If not, you may be out of luck.
Also, try sudo killall -9 lighttpd which sends the specific signal KILL instead of TERM.
Just to be sure you can also try sudo kill -9 614 using the PID.
Is the task written in the ps aux list in brackets? If so, it is a zombie, it is waiting some I/O task, which probably never completes. You can't kill it as far as I know.
Does it belong to you ? If you do
ps aux | grep lighttpd
that will give you the user id associated with that process (I'm guessing it's chowned to another user)
It works: killall -u root -c lighttpd
Try
sudo kill -9 `pgrep lighttpd`

Resources