when using netstat query port, always can not see the name of the program, you must also cooperate with PS to the process number intersection, a two can, often need to see the words are silly, this script is to solve such a pain point, especially to optimize the Java program memory.
on a night of Black nights, I had a long-running awk code, and finally did it, and after several optimizations, the execution was very fast.
#!/bin/Bashnetstat-tunlp2>/dev/NULL|awk-V page= ' getconf page_size '-F"[/ ]+" '$ (NF-1) = = "Java" {Sub"^0.0.0.0","*",$4) printf"%s%s%20s%s%6s",$1,$6~/[0-9]+/?"Unconn":$6,$4, $ (nf-1), $ (nf-2) System ("r= (' </proc/"($ (nf-2))"/statm ');p rintf '%5s ' $[${r[1]}*"PAGE"/1048576]")) printf"M"System ("readlink-f/proc/"($ (nf-2))"/CWD"))}'
Process parsing:
Display the TCP and UDP sockets for the system: NETSTAT-TUNLP:
To assign a memory page size to Awk's page variable:-v page= ' getconf page_size '
Replace 0.0.0.0 with an asterisk: Sub ("^0.0.0.0", "*", $4)
Print process number, socket, listening status: printf "%s%s%20s%s%6s", $1,$6~/[0-9]+/? " Unconn ": $6,$4,$ (NF-1), $ (NF-2) calls the system command process memory Usage: Systems ((" R= "($ (</proc/)) ' NF-2 ');p/statm ' rintf '%5s" Page "/1048576]"))
Call the system command to query the name of the Java process: System ("Readlink-f/proc/" ($ (NF-2)) "/CWD")
did you write a good article? Please sweep the author's begging code below to encourage the author to continue sharing
A simple script that shows the Java process: port, program name, process number, memory usage