Search code examples
linuxbashshelldiskgnu-parallel

check disk space while copying in shell script


I am copying files in parallel using GNU parallel as shown below and it works fine.

export COPY_PRI=/abc/pri
export COPY_SEC=/pqr/sec

export status_dir=$(mktemp -t -d transfer.XXXXXX)
cleanup() { rm -rf "$status_dir"; }
trap cleanup 0

copy() {
  el=$1
  PRIMSEC=$2
  scp bullseye@"$MACHINE_ONE":"$dir3"/process_"$el"_210_5.data "$PRIMSEC"/. || { touch "$status_dir/local_down" && scp bullseye@"$MACHINE_TWO":"$dir3"/process_"$el"_210_5.data "$PRIMSEC"/.; } || { touch "$status_dir/pri_down" && scp bullseye@"$MACHINE_THREE":"$dir3"/process_"$el"_210_5.data "$PRIMSEC"/.; } || { touch "$status_dir/sec_down" && exit 1; }
}
export -f copy

parallel -j 3 copy {} $COPY_PRI ::: "${PRI_FILES[@]}" &
parallel -j 3 copy {} $COPY_SEC ::: "${SEC_FILES[@]}" &
wait

Now I want to check disk space before copying any new files. If disk has 20% space left, then only copy if it doesn't have 20% space left, then exit out of the shell script with an error message and non zero status code. So how can I check disk space after every scp copy and make sure it is not reaching the limits? I have to check /abc and /pqr disk. They are two different disk.


Solution

  • Run something like this in the copy function before calling scp:

    df /abc /pqr | awk 'NR>1 {gsub(/%/,""); if ($5 > 80) {exit 1}}' || exit 1
    

    Explained (as requested):

    • df /abc /pgr: Get disk usage report for mounts /abc and /pgr
    • | awk: pipe to awk interpreter, awk script follows
      • NR>1 { ... }: execute this block for row numbers > 1 (i.e. ignore header row)
      • gsub(/%/,""): replace percent sign with nothing so that it does not ruin the numerical comparison
      • if ($5 > 80) { ... }: if fifth field ($5) on row is bigger than 80, execute this block of code
      • exit 1: exit awk with return value 1 to signal error
    • || exit 1: propagate the exit value of awk to the exit value of the script