在磁盘已满之前暂停进程
Pause a process before disk is full
某些进程(如 git gc --aggressive
)需要很长时间才能 运行,占用大量磁盘 space,如果我 运行 磁盘不足 space。如果磁盘 运行 很快用完 space,我想暂停它们,这样我就有时间释放一些内存。我该怎么做?
这是我想出的初步解决方案。使用 Mac OS X 进行测试。欢迎提出建议!
#!/bin/bash
# Change these variables as necessary.
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=
STOPAT=
# 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='3[0;31m'; PURPLE='3[0;35m'; BLUE='3[0;36m'
NC='3[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
某些进程(如 git gc --aggressive
)需要很长时间才能 运行,占用大量磁盘 space,如果我 运行 磁盘不足 space。如果磁盘 运行 很快用完 space,我想暂停它们,这样我就有时间释放一些内存。我该怎么做?
这是我想出的初步解决方案。使用 Mac OS X 进行测试。欢迎提出建议!
#!/bin/bash
# Change these variables as necessary.
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=
STOPAT=
# 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='3[0;31m'; PURPLE='3[0;35m'; BLUE='3[0;36m'
NC='3[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