Linux Ubuntu uses shell scripts to monitor the memory usage and CPU usage of a single process

Source: Internet
Author: User

1. Create a monitoring script
First, create an empty document in. Sh format under the folder.
Enter a command under the root permission of the terminal to open the. Sh file and edit it.
Sudo Vim /*/*...... /**. Sh // press the I key when editing the input in Vim. After editing, Press ESC to exit the edit option. Then, enter WQ IN THE uneditable state to save and exit.

// Shell script
PID = 'pidof processname' // The quotation mark is not a single quotation mark and is input by the key under the keyboard ESC. Obtain the ID of the process.
Echo $ PID // ID output to the terminal interface
Interval = 10 // record every 10 seconds
While true
Do
Echo $ (date + "% Y-% m-% d % H: % m: % s")> /home/user/share/test/proc_memlog.txt // obtain the system time

CAT/proc/$ PID/status | grep-e vmrss>/home/user/share/test/proc_memlog.txt // write the memory usage vmrss to the directory/home/user/share/ in the proc_memlog.txt file under test. Proc_memlog.txt is an automatically generated file. ">" Indicates append writing. The written content is not overwritten.

CPU = 'top-N 1-p $ PID | tail-2 | head-1 | awk' {SSD = NF-4} {print $ SSD} ''// obtain the process's CPU usage.
Echo "CPU:" $ CPU>/home/user/share/test/proc_memlog.txt // write the CPU usage rate to the proc_memlog.txt file.
Echo $ blank>/home/user/share/test/proc_memlog.txt // write a blank row into the proc_memlog.txt File
Sleep $ Interval
Done
// Run the script
2. Monitoring script
Run the terminal, switch to the directory where the script is created, and enter sh./***. Sh // to monitor the memory and CPU.

3.Because the proc_memlog.txt file has been written for a long time, the file will be too large. In this case, proc_memlog.txt can be modified to kb for each write, and another file is automatically generated to continue writing. The following URL contains a complete script, which splits the file by kb.

Http://blog.chinaunix.net/uid-16728139-id-3415706.html

Related Article

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.