Check how much disk space is left using AppleScript
This is a fairly simple one but someone out there may need this. My boss recently had a weird problem where his disk was suddenly filling up. A reboot fixed it but we were having a hard time finding the process that was suddenly eating 15 GB of disk space with no warning. So, I came up with this script that just gets how much free space is left on his disk. We ran it as a cron job every 15 minutes so we could hopefully get some warning before things filled up and became unusable. The script displays a warning when the disk has 5% or less of free space. You can edit it for your own uses. It also has the ability to email the report to you. Comment out the section you don’t want to use. To have it send emails when you are not logged in use something like Lingon for a launchd item or Cronnix for a cron job. Save your script in the appropriate location and have the launchd/cron job call it using the “osascript” command. So, if the file was saved in the /Users/Shared folder the line would read:
osascript /Users/Shared/disk_check.scpt
[codesyntax lang=”applescript”]
set mysubject to “Disk Usage Report”
set myrecipient to “sysadmin@example.com”
–get the amount of free space
set dSize to (do shell script “df -h / | grep %”) as text
–pull out the percent used from the result
set theTotal to word 6 in dSize as number
–if the total used space is 95% or more put up a warning. Comment out if running the email section
if theTotal is greater than or equal to 95 then
display dialog “Your disk is ” & theTotal & “% full”
end if
–comment out this section if no email is desired.
set mybody to (“Free Space: ” & theTotal & “%”)
do shell script (“echo \”” & mybody & “\” | mail -s \”” & mysubject & “\” ” & myrecipient)
[/codesyntax]
Click here to download a copy: Disk Check script
webmaster :: Oct.14.2010 :: Applescript, Management, Scripts :: 4 Comments »
4 Responses to “Check how much disk space is left using AppleScript”
I need something similar and maybe you can point me in the right direction. I have a mac server with some mounted shares, and two partitions on the physical machine. I want to write a script that will send me an email once a week with the current usage of all mounted shares and hard disk partitions. Any ideas? Thanks!
Gabe – a good thought. I’ve updated the code and the script to incorporate sending an email as an option. You can get a list of mount point paths using “diskutil list” and then adapt the script to check them all. I adapted the sendmail section from this article at Mac OS X Hints
This has been extremely helpful. How can I adapt this to check all the drives in my mac pro?
Running OS X Mavericks, I had to change this line:
“set theTotal to word 8 in dSize as number”
to
“set theTotal to word 19 in dSize as number”