Mir ist aufgefallen, dass mein Cron-Skript nach der Ausführung fast eine Stunde lang alle 1 oder 2 Minuten weiterläuft. Da ich viele Cron-Jobs habe, kann ich manchmal nicht auf meine Website zugreifen und der Browser sucht sehr lange:
# Check disk space each 5 AM everyday
* 5 * * * /root/Scripts/Misc/disk_space.sh
disk_space.sh überprüft den Speicherplatz in jeder Partition und benachrichtigt mich per E-Mail, wenn der Speicherplatz größer als 95 % ist
disk_space.sh
#!/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
Danke für eure Unterstützung
Antwort1
Ihre Cron-Syntax ist falsch. * bedeutetjeden, daher wird es zwischen 5 und 6 Uhr jede Minute ausgeführt. Beispiel: 5:23 stimmt überein, weil 5 mit 5 übereinstimmt und 23 mit * übereinstimmt.
Sie möchten eine Null im Minutenfeld, sodass es nur einmal um genau 5 Uhr morgens ausgeführt wird.
Korrigierte Zeile:
0 5 * * * /root/Scripts/Misc/disk_space.sh