Cron 每隔 1 或 2 分钟运行一次

Cron 每隔 1 或 2 分钟运行一次

我注意到我的 cron 脚本在执行后每 1 或 2 分钟就会运行一次,持续了将近 1 个小时,因为我有很多 cron 作业,有时我无法访问我的网站,浏览器会持续搜索很长时间:

# Check disk space each 5 AM everyday
* 5 * * * /root/Scripts/Misc/disk_space.sh

disk_space.sh 将检查每个分区的磁盘空间,如果磁盘空间大于 95%,将通过电子邮件通知我

磁盘空间

#!/bin/bash

#output=$(df -h | awk '{ print $5 }')

# Define date and time
Now=$(date +"%d-%m-%Y %T")

# Define hostname
hostname=$(hostname)

# Set alert limit, 95% we remove % for comparison
alert=95

# $1 is the partition name
# $5 is the used percentage
# NR>2: Start reading from row 2, otherwise will start reading from Strings (Use%)
# result of df -h will start output from Filesystem      Size  Used Avail Use% Mounted on
# and we cannot compare strings to digits while using -eg

# Print the df -h, then loop and read the result 
# NR>2: start reading from horizontal line number 2
df -h | awk 'NR>2 { print $1 " " $5 }' | while read output;
do

#echo $output

# Get partition name
partition=$(echo $output | awk '{ print $1 }')
#echo 'Partition: ' $partition
# Used space with percentage
useSpPer=$(echo $output | awk '{ print $2}')
#echo 'Used space %: ' $useSpPer

#used space (remove percentage)
useSp=$(echo -n $useSpPer | head -c -1)
#echo 'Used space digit: ' $useSp

#useSpDg=$(sed '1d' $useSp)
#echo $useSpDg

# Recap
#echo $useSp ' has ' $partition

# -ge is greatter than or equal
#echo "DEBUG [$useSp] [$alert]"

if [ $useSp -ge $alert ]; then
#echo $partition 'is running out of space with '$useSpPer

echo $Now ' ' $partition ' ' $useSpPer >> /root/Scripts/Misc/disk_space.log

dfRes=$(df -h)

# Inform the admin
echo -e "The partition $partition belongs to the host: $hostname is running out of space with $useSpPer.\n\nThis is the full result: \n\n$dfRes \n\nThis message is a warning to take an action, sent on $Now \nThank you for using this script" 2>&1 | sed '1!b;s/^/To: MAILID\nSubject: '$hostname': Disk Space\n\n/' | /usr/sbin/sendmail -t
#else
#echo 'Down'

fi

done
#echo $output

感谢您的支持

答案1

您的 cron 语法不正确。* 表示每一个,因此在凌晨 5 点到 6 点之间,它将每分钟运行一次。例如 5:23 场比赛,因为 5 场比赛 5 和 23 场比赛 *。

您希望在分钟字段中输入零,以便它只在凌晨 5 点准时运行一次。

更正后的行:

0 5 * * * /root/Scripts/Misc/disk_space.sh

相关内容