3

Sometimes I need to kill a process (the reasons why are not important). And I know I can find that process with the following command: lsof -i :8080, being my candidate the last process in the output table.

For example, if I run the command, an output like this will appear:

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 15112 dani 70u IPv4 3178183 0t0 TCP localhost:39998->localhost:http-alt (CLOSE_WAIT) java 15112 dani 137u IPv4 3181607 0t0 TCP localhost:39999->localhost:http-alt (CLOSE_WAIT) java 15112 dani 138u IPv4 3181608 0t0 TCP localhost:40000->localhost:http-alt (CLOSE_WAIT) java 15112 dani 139u IPv4 3181609 0t0 TCP localhost:40001->localhost:http-alt (CLOSE_WAIT) java 15112 dani 140u IPv4 3181610 0t0 TCP localhost:40002->localhost:http-alt (CLOSE_WAIT) java 19509 dani 50u IPv6 4617361 0t0 TCP *:http-alt (LISTEN) java 19509 dani 52u IPv6 6642445 0t0 TCP localhost:http-alt->localhost:42996 (CLOSE_WAIT) 

So, my target will be that PID 19509. Using pipes, how can I cherry-pick last line's PID?

I want to reach a command like lsof -i :8080 | something here to get the PID | kill -9

I'm running Linux Mint KDE x64

    1 Answer 1

    4

    Answering your question literally, here's one way to list the last PID displayed by lsof:

    lsof … | awk 'END {print $2}' 

    Awk is a text processing language which reads input and processes it line by line. In the code, END {…} executes the code in the braces after the whole input is processed, and effectively operates on the last line. $2 is the second whitespace-delimited field on the line.

    And here are some ways to kill it (each line works on its own):

    kill $(lsof … | awk 'END {print $2}') lsof … | awk 'END {print $2}' | xargs kill lsof … | awk 'END {system("kill " $2)}' 

    However, I dispute your assertion that the right process to kill is always the last one. lsof displays processes by increasing PID, which is meaningless. Even on systems where process IDs are assigned sequentially (which is not the case on all Unix variants, not even on all Linux installations), they wrap once they reach the maximum value (commonly 32767). Thus deciding between processes by comparing PIDs is meaningless.

    You need some other information to decide which process to kill. Depending on what kind of information you're after and on whether you might have output that contains “weird” characters (like spaces in file or program names), you may use a tool like awk to process the output of lsof, or you may use the -F option to lsof which produces output that's a bit harder to parse in simple cases but (almost) not prone to ambiguity and easier to parse robustly. For example, if you want to kill any process that's listening on port 8080, here's how you can do it:

    lsof -n -i :8080 -F | awk ' sub(/^p/,"") {pid = $0} $0 == "n*:http-alt" {print pid} ' | xargs kill 

    The call to the sub function replaces p at the beginning of a line by an empty string. If this replacement is performed, the code block {pid = $0} is executed; this way the pid variable contains the last PID value displayed by lsof. The second awk line prints the value of the pid variable if the line is exactly "n*:http-alt", which is lsof's way to report a socket listening on port 8080 on all interfaces.

    This particular criterion actually doesn't require any parsing (I only showed it above as an example). You can make lsof display just processes listening on the specified port:

    lsof -n -a -iTCP:8080 -sTCP:LISTEN -Fp | sed 's/^p//' | xargs kill 

    Or, for this, you can use netstat instead.

    netstat -lnpt | awk '$4 ~ /:8080$/ {sub(/\/.*/, "", $7); print $7}' 

    Explanation of the awk code: if the 4th column ends with :8080, replace everything after the first / in the 7th column (to remove the process name part and keep only the PID part), and print it.

    1
    • Really complete and extremely usefull answer. I really appreciate the verbosity. After reading all the alternatives you gave me, I chose the lsof -n -a -iTCP:8080 -sTCP:LISTEN -Fp | sed 's/^p//' | xargs kill. It seems to be the most accurate for my purposes. Also, in my simple mind there is a meaning for that process to be always at the bottom of the table: Is a process which is started/stoped very often and, sometimes, crashes (and then I need to kill it), so in most cases will be the last in the table. But, also, I take'll take into account the maximum of 32767 PID numbers, as you said.CommentedMay 2, 2015 at 0:50

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.