shell script to monitor disk usage
How about this start:
du -sk <full-path-to-dir-to-watch>/* | sort -nr | head -n 10
du command will give you disk usage in blocks for all files/folders in that place you should be watching, sort with -n will sort by first column (usage figures), -r will reverse (print largest numbers 1st), then head will give you the first 10 items, the largest.
Put this in the shell script, etc... (you know better what else you wish to do)
Bash monitor disk usage
#!/bin/bash
source /etc/profile
# Device to check
devname="/dev/sdb1"
let p=`df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }'`
if [ $p -ge 90 ]
then
df -h $devname | mail -s "Low on space" my@email.com
fi
Crontab this to run however often you want an alert
EDIT: For multiple disks
#!/bin/bash
source /etc/profile
# Devices to check
devnames="/dev/sdb1 /dev/sda1"
for devname in $devnames
do
let p=`df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }'`
if [ $p -ge 90 ]
then
df -h $devname | mail -s "$devname is low on space" my@email.com
fi
done
Check free disk space for current partition in bash
Yes:
df -k .
for the current directory.
df -k /some/dir
if you want to check a specific directory.
You might also want to check out the stat(1)
command if your system has it. You can specify output formats to make it easier for your script to parse. Here's a little example:
$ echo $(($(stat -f --format="%a*%S" .)))
Is there a shell script that can monitor partition usage?
don't know if there's already one, but it's not too hard to write. Just put this into your crontab:
df | awk 'NR>1 && $5>80 {print $1 " " $5}'
You should replace 80 with the threshold (percent used) you want to be alerted on. If will mail you the df output for all partitions that cross that usage level.
Related Topics
Write and Read from Ttyusb0, Can't Get Response
Linux - Modify File Modify/Access/Change Time
Why Are Both "True" and "False" Tests True
Make Bash Differentiate Between Ctrl-<Letter> and Ctrl-Shift-<Letter>
Git Post-Receive Checkout to Remote Machine
Ubuntu/Fedora: How to Add Applications in The Menus
Linux/Unix Socket Self-Connection
Linux: Update Embedded Resource from Executable
How to Launch Linux Subshell for Ssh_Askpass to Work in Linux
List of Files Modified 1 Hour Before
What Are The Lowest Possible Permissions for Typo3
Find Based Filename Autocomplete in Bash Script
Webdrivererror Error: Chrome Failed to Start: Exited Abnormally
How to Manipulate Array in Shell Script
Does Each Unix File Description Have Its Own Read/Write Buffers