7

I am trying to write a script that kills service running at a specific port. This is my script:

a=$(ps ax | grep 4990 | grep java | awk '{print $1}')
kill -9 $a

It's a java program. This script works sometimes, but mysteriously fails most of the time.

Is there any other way to kill a service running on a port? My port is 4990.

  • Your script would also kill Java processes running on other pets such as 14990. – roaima Jan 22 at 8:45
23

You can try fuser:

fuser -k 4990/tcp

Or using lsof to get the process id, then feed to kill:

kill $(sudo lsof -t -i:4990)
  • Should I have to replace tcp with java? – Abhishek dot py Jul 3 '14 at 7:20
  • @Abhishekdotpy: No, use tcp. – cuonglm Jul 3 '14 at 11:46
  • Thank you for that -t flag, I have needed that for so long. – Jackson Jun 6 '15 at 8:04
3

If you want to kill any service or process running on port number 8080 then first you need to find the 8080 port process identification number(PID) and then kill it. Run the following command to find 8080 port number PID:

sudo lsof -t -i:8080

Here,

  • sudo - command to ask admin privilege(user id and password).
  • lsof - list of files(Also used for to list related processes)
  • -t - show only process ID
  • -i - show only internet connections related process
  • :8080 - show only processes in this port number

So you can now easily kill your PID using following command:

sudo kill -9 <PID>

Here,

  • kill - command to kill the process
  • -9 - forcefully

You can use one command to to kill a process on a specific port using the following command:

sudo kill -9 $(sudo lsof -t -i:8000)

For more you can see the following link How to kill a process on a specific port on linux

  • Using kill -9 gives the target process no opportunity to tidy up. Consider using plain kill, at least initially. – roaima Jan 22 at 8:43
1
<?php
  $port = $argv[1]; 
  if(!empty($port) && is_numeric($port) ){
  $pid = trim(shell_exec("lsof -t -i:".$port));
  if(!empty($pid)){
     shell_exec("sudo kill -9 ".$pid);
     echo "pid : ".$pid."  killed which is listened port : ".$port."\xA";
  }
  else{
     echo " [X] no such process found for pid : ".$port."\xA";
  }
  }
  else{ 
     echo " invalid pid \xA";
  }
?>
  1. create a php file kill.php
  2. run the php file with required port $ php kill.php 8088
  • 1
    Using kill -9 gives the target process no opportunity to tidy up. Consider using plain kill, at least initially. – roaima Jan 22 at 8:44
  • +1 for using PHP for this task. – Bananguin Jan 22 at 8:51
  • @G-Man by mistake it added – nivas Jan 23 at 5:45
  • @Bananguin to kill exact process id of the given port am using this when executing bunch process with jenkins – nivas Jan 23 at 5:52
  • when running script files and its contain lsof -t -i:port_id command when build process the port_id or process id of port_id may be empty, that time we get some command error, to avoid that kind of error am used this. here i can kill exact process id of the given port – nivas Jan 23 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.