A simple script that shows the Java process: port, program name, process number, memory usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.