sh 用于检查启动分区是否有足够剩余空间的脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 用于检查启动分区是否有足够剩余空间的脚本相关的知识,希望对你有一定的参考价值。

#!/bin/bash 
# call this file in your profile as background job

set -e

# Adjust this value if it's too long or too short to wait
sleep 600

# You need to change this path for your own directory
BASE_DIR=/path/to/your_notify_dir
cd $BASE_DIR
SCRIPT=./notify_when_boot_full.sh
LOG_FILE=./notify_boot_in_profile.log
touch $LOG_FILE

$SCRIPT 2>&1 >>$LOG_FILE &

exit 0 
#!/bin/bash 

# The scripts may or may not work, depending on your environment.
# The boot partition should be mounted as /boot (Run df -h to check), otherwise 
#     you should change the part `grep /boot$` in the script yourself.

# You need to move the script files to the /path/to/your_notify_dir, with proper permissions:
# notify_when_boot_full.sh
# notify_boot_in_profile.sh (See the other script below this one)

# INSTRUCTIONS ON LOGIN USAGE with .profile
# gedit ~/.profile
# add the following line at the bottom of the profile file:
# /path/to/your_notify_dir/notify_boot_in_profile.sh & 
# (the & above is required, and also change the path to your script)
# The script will wait 10 minutes after you login by default
# If the script couldn't work in ~/.profile as background job &, check permissions and the log.

# You can also try cron job.
# Just change the file name notify_boot_in_profile.sh to notify_boot_in_cron.sh, as well as its content accordingly.


set -e

# sleep 600

# Change the THRESHOLD to the minimal boot space you alert
THRESHOLD=400
TARGET_USER=$USER

# Uncomment the next line if you try to use cron jobs. Use your login name to replace your_login_user_name 
#TARGET_USER=your_login_user_name

# YearMonthDay.HourMinute
datef=`date '+%Y%m%d.%H%M'`
echo 
echo $datef $0

# Get available boot memory in mega bytes, e.g., 100M
avail_m=$(df -h | grep [[:blank:]]/boot$ | awk '{ print $4 }')
echo $avail_m

# Change available boot memory to pure number without units, e.g., 100
avail_basic=${avail_m::-1}

# Notify the $TARGET_USER if available memory is below the $THRESHOLD
if [ $avail_basic -le $THRESHOLD ]
    then
    # adjust the environment value
    eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $TARGET_USER gnome-session)/environ)"
    #export DISPLAY=:0
    /usr/bin/notify-send "Boot disk is low: $avail_m"
fi

exit 0

以上是关于sh 用于检查启动分区是否有足够剩余空间的脚本的主要内容,如果未能解决你的问题,请参考以下文章

正在保存备份的磁盘没有足够的可用空间

文件组 'PRIMARY' 已满 解决办法

shell监控脚本----硬盘空间和内存

如何从VHD启动WIN10

空间不足时将数据写入文件开头

Linux学习—脚本练习题