how to stop http-server on a specific port? - shell

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);

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.

kdb+ - how to start process without blocking the terminal in linux

I am trying to run couple of processes on linux of kdb+(TP, RDB, HDB)
e.g
q tick.q sym /mnt/disks/disk1/OnDiskDB/ -p 5000
The problem which I have is that I have to keep terminals opened to keep q processes running
Below are my steps:
I tried:
q tick.q sym /mnt/disks/disk1/OnDiskDB/ -p 5000 &
but then I have to manually hit enter to exit q prompt (which could be still ok), some info about processes stops and then I am closing terminal
[piotr#server tick-example]$ q tick.q sym /mnt/disks/disk1/OnDiskDB/ -p 5000 &
[1] 6627
[piotr#server tick-example]$ KDB+ 3.6 .....
q) (enter)
[piotr#server tick-example]$ (hitting enter)
[piotr#server tick-example]$ (hitting enter)
[1]+ Stopped q tick.q sym mnt/disks/disk1/OnDiskDB/ -p 5000
[piotr#server tick-example]$
But it seems that process is still running
[piotr#server tick-example]$ ps -efww | grep tick
piotr 6627 6408 0 14:55 pts/7 00:00:00 q tick.q sym /mnt/disks/disk1/OnDiskDB/ -p 5000
And now closing terminal
Opening another terminal for verification:
[piotr#server tick-example]$q
...
q)h:hopen `::5000
'hop. OS reports: Connection refused
[0] h:hopen `::5000
q)\\
[piotr#server tick-example]$ ps -efww | grep tick
...
nothing
You should read a bit more about how background and foreground processes run in a shell. Basically you are not detaching by adding a & at the end of your command, it still depends on your shell and your input.
Here is a down-to-the-point explanation on what you should use for different cases.
Running command & simply sends the command to bg so you can keep using your terminal
Running nohup command & > /dev/null is the safest combination if you want to keep you process running independently from your shell (replace /dev/null with whatever file you want).
Also read about the Job control commands. TLDP has a good article.
Try to add
nohup
before your commands
This issue is more of a unix/shell related than KDB as already mentioned by others. Below is one solution that you can try.
When you send a process to background and if that process is still waiting for an input from terminal then it goes into a stopped state. In that case it will not accept request from other KDB process. And that is what you are seeing.
To fix this, you need to change stdin to detach terminal input. Below command redirects output to some log file and also change stdin to /dev/null.
'nohup' command is used so that process will keep running even after terminal is closed.
nohup q -p 5000 >output.log </dev/null &
Now you can eaily connect from other kdb service:
q)h:hopen `::5000
q)h ".z.K"
q)3.5

Is there a Terminal command that I can use to shut down a mongod process in OS X?

mongod --shutdown is not available on OS X. The only good way seems to be using the mongo interface. Can I shut down a mongod process that was started in the background using the --fork flag using the command line? Any way to use kill with ps?
I'm trying to shut down MongoDB using an npm posttest script (just a command that's run after my unit tests).
Either find out the pid using ps | grep mongod or echo $! after starting it up (the variable $! holds the pid of the last started process) and use kill pid.
Or, simply use mongo admin --eval "db.shutdownServer()", which throws an error but seems to work anyway. You can always pipe undesired output to /dev/null.

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 an open process on node.js?

I'm trying to set up a build-system for Node.js on sublime, so I can press F7 to call "node" on the openned file. The problem is that the process is then open forever, so, the second time I use F7 I get an add-in-use.
Is there a way I can kill the openned "node.exe" process from node.js?
Use the following set of commands to identify the process running on a given port and to termiate it from the command line
sudo fuser -v 5000/tcp // gives you the process running on port 5000
It will output details similar to the one shown below
USER PID ACCESS COMMAND
5000/tcp: almypal 20834 F.... node
Then use
sudo fuser -vk 5000/tcp
to terminate the process. Check once again using
sudo fuser -v 5000/tcp
to ensure that the process has terminated.
On Windows you could use the following steps
C:\> tasklist // will show the list of running process'
Image Name PID Session Name Session# Mem Usage
System 4 console 0 236 K
...
node.exe 3592 console 0 8440 k
Note the PID corresponding to your node process, in this case 3592. Next run taskkill to terminate the process.
C:\> taskkill /F /PID 3592
Or /IM switch
C:\> taskkill /F /IM node.exe
From within Node.js:
var die = function(quitMsg)
{
console.error(quitMsg)
process.exit(1);
}
die('Process quit');
There are certain methods available for exiting that are only available for POSIX (i.e. not Windows) that will exit a process by its process id.
Also, note that you might be able to send a kill() signal using this method, which does not say it isn't available for Windows:
process.kill(pid, [signal])
If you want to kill all processes than:
sudo killall -9 node
If you want to kill process on selected port than:
sudo kill sudo lsof -t -i:3100
That was port 3100
If sublime you say is sublimeText plugin, I have the same issue, and send TCP server a message 'shutdown' from python code, then
app.js
TCPserver
.on('connection', function(socket)
{
socket.pipe(require('through')
(function(data)
{ //----------------------------
if (data.toString() === 'shutdown')
{
process.exit();
}
//--------------------------
}));
socket.end();
})
Similarly to what #Alex W said, you can send a kill signal to the process so long as you have its process ID, or PID using the following node function:
process.kill(pid, [signal])
In my case, I had the PIDs readily available as I was spawning child_process().spawn.pid. I have tested it and it does work on Win 7 x64.

Resources