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