UNIX Drive Space Warning Cron.
Using your favorite editor, create the following file as /usr/local/sbin/drivespace.sh
#!/bin/sh
# changeable variables:
ds=95 # Drive space percentage to worry about.
path="/dev/ad0" # Drive path to check.
emailTo="some@address.com" # Email address for the alerts.
# values is an array holding only the Capacity - a percentage of used space.
values="$(df | grep $path | awk '{print $5}' | sed 's/%//')"
# Loop through the above values. Check if any are larger than the drive space variable from above.
for value in $values
do
if [ $value -gt $ds ]; then
# Found. Email a warning then exit.
df -h | /usr/bin/mail -s "Drive Space Warning" $emailTo
exit
fi
done
Note that running the df command by itself gives the following output:
# df
Filesystem Size Used Avail Capacity Mounted on
/dev/ad0s1a 252M 93M 139M 40% /
/dev/ad0s1g 8.9G 442K 8.2G 0% /home
/dev/ad0s1e 8.9G 12K 8.2G 0% /tmp
/dev/ad0s1h 8.9G 609M 7.6G 7% /usr
/dev/ad0s1f 8.9G 12M 8.1G 0% /var
/dev/ad0s1d 36G 500M 32G 1% /web
procfs 4.0K 4.0K 0B 100% /proc
Whereas df | grep $path | awk '{print $5}' | sed 's/%//' gives only:
# df | grep /dev/ad0 | awk '{print $5}' | sed 's/%//'
40
0
0
7
0
1
Next change the permissions on the file to allow for execute.
# chmod 755 /usr/local/sbin/drivespace.sh
We can test the file by directly running the script from the command line.
# /usr/local/sbin/drivespace.sh
#
No output should occur. IF there is a drive space issue, check the email account entered within the code.
Once we are satisfied as to its function, we can then add this to the crontab for daily execution.
# crontab -e
Esc menu ^P prev page ^K del char ^O end of lin ^Y adv word ^G^P prev buff ^G^V forward
^E command ^L del line ^G^K und char ^U mark ^Z replace ^G^X fmt parag ^G^R reverse
^T top of txt ^G^L und line ^F search ^X cut ^G^Z repl prmpt ^G^X fmt parag ^G^B append
^B end of txt ^W del word ^G^F srch prmpt ^C copy ^G^C clear line ^A adv char ^G^D prefix
^N next page ^G^W und word ^D beg of lin ^V paste ^G^N next buff ^G^Y prev word ^R redraw
^ = Ctrl key ---- access HELP through menu ---============================================================
The format for the date/ time of execution is as follows:
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (1 - 7) (monday = 1)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
So to execute hourly, on the hour, we would add the following:
0 * * * * /usr/local/sbin/drivespace.sh
After adding the above and saving, you can run the following to verify:
# crontab -l
# Drive space checker running hourly
0 * * * * /usr/local/sbin/drivespace.sh