繁体   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