Shell script: Write data to disk in bulk

Source: Internet
Author: User
Tags shebang

Shell script: Write data to disk in bulk

First, about this article

The monitoring system to be done requires monitoring the usage of disk space and alerting. When testing this feature, it is necessary to simulate two scenarios: one is the need to trigger the alarm mechanism when the disk space is constantly growing above the set threshold, and the second is that the disk space drops below the alarm threshold and no more alarms. To test these two scenarios, I wrote the following three scripts:

1) initializer.sh: Create a directory TestDir, and create a large file template

2) duplicator.sh: Copy the file template continuously until the disk space usage exceeds the input parameters

3) cleaner.sh: Remove the traces left by the previous two scripts, i.e. delete the directory TestDir

Second, initializer.sh

The first way to create a large file is to write the characters to the file and copy them through the shell, with the code as follows (Initializer.sh.old):

#!/bin/sh#本脚本用于初始化工作, create a folder TestDir and write to a directory of size 100M#创建文件TestDirIf[-X"./testdir"];ThenRM-RF TestDirFimkdir testdircd testdirmkdir templatecd Templatetouch template.txt#制作大小为100K的文件template. txtstring="" Repetend="012345678901234|"For ((i=1;i<6400;i++))Dostring=$string $repetend doneecho $ String >> template.txt #复制1000个该文件i =0while [true]; do if [ " $i"-GT 1020]; then  break  fi CP template.txt  $i  ((i++)) doneecho exit 0              

This is less efficient and the code is longer, and then the DD command is used to implement the function (initializer.sh):

#!/bin/sh#本脚本用于初始化工作, create a folder TestDir and write a directory of size 100M #创建文件TestDirthen rm-rf TestDirIf=/dev/zero of =template bs=1m count=1024pwd. Du-sh. cd. exit 0     

This script creates the TestDir directory and writes a 1.1GB file template inside it.

Third, duplicator.sh

The script duplicator.sh accepts a 5-95 number as the threshold value. This script keeps copying the template file created by initializer.sh until the disk space usage specified above the input threshold is exceeded.

#!/bin/sh#运行本脚本前请先运行脚本 initializer.sh#本脚本用于不断复制文件 until the given parameter threshold exceeds the current disk space utilization#输入参数: Disk space usage Thresholds#函数: Print Script usage description function usage () { Echo"Usage:./duplicator [Threshold]" Echo"Threshold is a integer in the range of [1,99]" Echo"*run initializer.sh before Run this script" Exit 0}#脚本有且只有一个输入If["$# "-ne 1];Then Echo"The script should have only one input"UsageFiPositive integer threshold= between #脚本的输入必须为5-95 'Echo$ | BC 'If["$threshold "-lt 5-o"$threshold "-GT 95];Then Echo"The input of the script must be a positive integer between 5-95"UsageFi#目录TestDir必须存在If[!-D./testdir];Then Echo"Missing directory TestDir"UsageFi#文件TestDir/template must existIf[!-F./testdir/template];Then Echo"Missing file Testdir/template"UsageFicd TestDir#复制文件, up to the threshold value entered i=0While[True];DoCur= ' Df-h | Grep/dev/sda3 | Awk' {printf substr ($5,1,length ($ 1)} ' Echo"Current usage:$cur | Object usage: $threshold " if [ "$cur "-gt "$threshold "]; Then break   ;  fi cp template $i echo " $i duplication complete!" ((i++))Donecd. #TestDirecho "Script finished!" exit 0                  
Iv. cleaner.sh

This script is used to clear traces of the first two scripts left in the system.

#!/bin/sh#本脚本用于清空脚本initializer. Sh and duplicator.sh left traces#检查文件是否存在If[!-X"./testdir"];Then Echo exit 0 fi #用户确认后清除文件 echo  " Do you really want to erase all the data? (y/n) "Read inputcase  $input "in  y* | y*)   Rm-rf./testdir  echo  "data delete complete";;  n | N)   echo  "discard delete data";;  *)   echo  "input not recognized";; Esacexit 0             
V. Effect of the call

END
    • Related articles recommended:
    • Script to monitor disk space usage
    • Linux how to view disk IO status
    • Shell scripts that monitor the health of remote disks
    • This article from: Hobby Linux Technology Network
    • This article link: http://www.ahlinux.com/shell/9024.html

Shell script: Write data to disk in bulk

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.