簡體   English   中英

在磁盤已滿之前暫停進程

[英]Pause a process before disk is full

某些進程(如git gc --aggressive )需要很長時間才能運行,會占用大量磁盤空間,並且如果我用完磁盤空間就會死掉。 因此,如果磁盤空間即將用盡,我想暫停它們,以便有時間釋放一些內存。 我怎樣才能做到這一點?

這是我想出的一個初步解決方案。 經過Mac OS X測試。歡迎提出建議!

#!/bin/bash
FILESYSTEM="/dev/disk1"
DF=/usr/local/opt/coreutils/libexec/gnubin/df
OSASCRIPT=/usr/bin/osascript

if ! [[ -x $DF ]]; then echo "Error: $DF isn't executable."; exit 1; fi

PID=$1
STOPAT=$2

# Verify input
if [[ -n ${PID//[0-9]/} ]]; then echo "Error: The first parameter should be an integer"; exit 1; fi
if [[ -n ${STOPAT//[0-9]/} ]]; then echo "Error: The second parameter should be an integer"; exit 1; fi

RED='\033[0;31m'; PURPLE='\033[0;35m'; BLUE='\033[0;36m'
NC='\033[0m' # No Color

echo -e "Will pause the following process when there are ${PURPLE}$STOPAT${NC} bytes left on ${PURPLE}$FILESYSTEM${NC}"

PROCESS=`ps -p $PID | grep $PID`
echo -e "${BLUE}$PROCESS${NC}"

# Check every second to see if FILESYSTEM has more than STOPAT bytes left.
while true; do
    left=`$DF | grep -m 1 $FILESYSTEM | tr -s ' ' | cut -d" " -f4`
    echo -ne "$left bytes left\r";
    if [[ $left -lt $STOPAT ]]; then
        MSG="pausing process...$PID";
        echo $MSG;
        if [[ -x $OSASCRIPT ]]; then
             $OSASCRIPT -e "display notification \"$MSG\""
        fi
        kill -TSTP $PID
        break
    fi
    sleep 1s
done

您需要更改DF以匹配df (我沒有直接調用df ,因為在我的系統上別名為df -h 。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM