Script Application 16: Monitor and consume memory resources

Source: Internet
Author: User

role: monitoring or consuming memory resources, specifying the percentage that needs to be consumed

implementation: by writing a file of a certain size in memory


Demonstrate:

Direct enter to view the current percentage of memory

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/19/wKiom1cTTL7BpP-FAAAe8gFliAQ675.png "style=" float: none; "title=" 1.png "alt=" Wkiom1cttl7bpp-faaae8gfliaq675.png "/>


650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/16/wKioL1cTTXqR27-9AAAXIgBbp5A102.png "style=" float: none; "title=" 2.png "alt=" Wkiol1cttxqr27-9aaaxigbbp5a102.png "/>


Enter the percentage of memory you want to consume

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/19/wKiom1cTTL_DX0FmAAAmlqhipvk259.png "style=" float: none; "title=" 3.png "alt=" Wkiom1cttl_dx0fmaaamlqhipvk259.png "/>


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/19/wKiom1cTTL-hqz6iAAAZxJ3g2iw650.png "style=" float: none; "title=" 4.png "alt=" Wkiom1cttl-hqz6iaaazxj3g2iw650.png "/>


650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7F/16/wKioL1cTTXvQF1thAAAVeUN8jA0150.png "style=" float: none; "title=" 5.png "alt=" Wkiol1cttxvqf1thaaaveun8ja0150.png "/>


Code area:

#/bin/bash #  ly#  ------------------#  copyright 2014.11.25  lingyi  ([email protected])  qq:1519952564get_file_num () {big_num=$1small_num=$2num=$3file_num_ perc=$ ( echo  "${big_num}-${small_num}"  | bc ) file_num_moment=$ ( echo  "$ (   echo  "0$ (echo " scale=2; ${file_num_perc}/100 "&NBSP;|&NBSP;BC)"  ) *${num} "  | bc ) file_num_one=$ ( echo ${file_num_moment} | awk -F.  ' {print $ 1} '   ' file_num_two=$ ( echo ${file_num_moment} | awk -F.  ' {print $2} '  ) [[ ${file_num_two:0:1} -eq 0 ]] && file_num_two=${file_num_two : 1:1}[[ ${file_num_two} -ge 50 ]] && let file_num_one++echo ${ File_num_one}}clean_up () {&NBSP;UMOUNT&NBSP;${MEMORY_DIR}&NBSP;&AMP;&AMP;&NBSP;RM&NBSP;-FR&NBSP;${MEMORY_DIR}RM  -fr .test_memory_file_sum.file}if [[ ! -f /usr/bin/bc ]]; then echo  "Try to  install bc tool ...  "yum install -y bc >/dev/null 2> &1if [[ $? -ne 0 ]]; thenecho -e  "\033[1;31mtool: \" bc\ "  install failed !! \033[0m "exit 1fifitrap  ' clean_up &>/dev/null; tput cnorm; echo -e   "\033[0m";  exit 0 '  2echo -e  "\033[1;32minput the percentage that  you want, if empty then only monitor. " echo -e  "\033[1;32m\" ctrl + c\ " to exit." echo -ne  "\033[1;34minput the percentage[ " echo -ne  "\033[1;31meg:50 " echo -ne  "\033[1;34m]: " read keep_busy_percif [[  $keep _busy_perc ==  "q " ]] | |  [[  $keep _busy_perc ==  "Q" &nbSP;]];  then exit 0; fimemory_dir= ". memory.$ (date +"%y%m%d%h%m%s ")" mkdir -p ${ memory_dir}if [[ $? -ne 0 ]]; then echo  "Create memory test  dir failed !! ";  exit 1; fimemory_sum=$ (  free -m | awk  ' {print $2} ' |sed  -n  ' 2p '  ) mount -t tmpfs -o size=${memory_sum}m tmpfs ${memory_ dir}tput civiswhile :; domemory_used=$ (  free -m | awk  ' {print  $3} '  | sed -n  ' 2p '   memory_free=$ ( echo  "${memory_sum}-${memory_used} " | bc " memory_busy_perc=$ ( echo  "scale=2; $ ( echo " scale=4; ${ memory_used}/${memory_sum}*100 "&NBSP;|&NBSP;BC)/1"  | bc ) tput cup 00; tput  edecho -e  "\033[1;32mmemory sum :\033[1;34m$ (printf "%-8s " "  ${mEmory_sum} ") \033[1;32mm" echo -e  "\033[1;32mmemory used:\033[1;35m$ (printf "%-8s " "  ${memory_used} ") \033[1;32mm" echo -e  "\033[1;32mmemory free:\033[1;36m$ (printf "%-8s "   " ${memory_free}") \033[1;32mm "echo -e " \033[1;32mmemory perc:\033[1;31m$ (printf   "%-8s"   " ${memory_busy_perc}") \033[1;32m% "echo -ne " \033[0m "one=$ ( echo ${ memory_busy_perc} | awk -f.  ' {print $1} ') two=$ ( echo ${memory_busy_perc}  | awk -F.  ' {print $2} ') [[ ${two:0:1} -eq 0 ]] &&  two=${two:1:1}[[ ${two} -ge 50 ]] && let one++if [[  -z $ ( ls ${memory_dir} )  ]]; then      if  [[ ${keep_busy_perc} -gt ${one} ]]; thenecho -ne  "\033[1; 31minitializating ...\033[0m "file_sum=$( get_file_num ${keep_busy_perc} ${one} ${memory_sum} ) echo ${file_sum}  >.test_memory_file_sum.filefor  ((i=1; i<=file_sum; i++));  dodd if=/dev/zero  of=${memory_dir}/${i} bs=1m count=1 &done >/dev/null 2>&1wait       fielseif [[ ${keep_busy_perc} -gt ${one} ]];  thenfile_num=$ ( get_file_num ${keep_busy_perc} ${one} ${memory_sum} ) file_sum=$ (echo  "' Cat .test_memory_file_sum.file ' +${file_num}" &NBSP;|&NBSP;BC) Echo ${file_sum} >. test_memory_file_sum.filefor  ((i=file_num+1; i<=${file_sum}; i++));  dodd if=/dev/ zero of=${memory_dir}/${i} bs=1m count=1 &done >/dev/null 2>& 1waitfiif [[ ${keep_busy_perc} -lt ${one} ]]; thenfile_num=$ ( get_file_num  ${one} ${keep_busy_perc} ${memory_sum} ) if [[ ${file_num} -gt $ (cat .test_memory_file_sum.file)  ]]; thenrm -fr ${memory_dir}/*elsefor  ((i=0; i<file_num; i++));  Dothe_file_name= ' echo  "$ (cat .test_memory_file_sum.file)-${i}" &NBSP;|&NBSP;BC "rm -fr ${ Memory_dir}/${the_file_name}donefififisleep 1done




Accessories Download Source Code


This article is from the "retrograde person" blog, please be sure to keep this source http://lingyi.blog.51cto.com/2837715/1764783

Script Application 16: Monitor and consume memory resources

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.