| By Dave Taylor | Article Rating: |
|
| June 15, 2004 12:00 AM EDT | Reads: |
12,766 |
Q: AnswerSquad, I need your help. Try as I might, I can never seem to keep my users from sucking up all the available disk space. I know I could try using the quota system on my Linux box to keep people in check, but I know my users and know that they'd just get cranky about the system enforcing limits. Instead, I'd like to just have some automated way to send them pestering e-mail if they're using more than their fair share of disk space. But I don't see any existing utility to do this. Help!
One of the classic Unix admin tasks is figuring out who is taking up all the disk space. With MP3 libraries, downloadable movies, and more massive file sources online than we can shake a stick at, it doesn't take much time for that 40GB disk to seem pretty small, does it?
The standard way to calculate disk space usage is to use the du command and have it report on all the subdirectories of your account home directory (often /home):
$ cd /home
$ du -s *
52453 admin
9374 taylor
14325497 mitchell
8498374 citarella
You can get a good idea of disk usage here, but it's not always obvious what the number reports. A quick peek at the du man page reveals that on this version of Linux the numbers are reported in 1K blocks, so admin is using 52MB, taylor's using 9MB, mitchell is eating up lots of space with 14.3GB, and citarella's also grabbing more disk space than might be necessary with 8.49GB. (Actually, some versions of du have a helpful "-h" flag that makes its output more readable. Others have a "-k" flag that force 1K blocks as the unit of calculation.)
Transforming Commands into Shell Scripts
To turn this into a useful shell script - and while bash is the default shell in Linux, we'll use /bin/sh for scripting because it's the standard scripting environment, and so you could copy this script over to any other Unix or Linux system - simply create a plain text file that contains the commands necessary to feed the du output into an awk snippet that can test sizes and calculate MB rather than KB:cd /home
du -s * | \
awk '$1 > 500000 { MB = $1 / 1024 ; print $2 "=" MB "MB" }'
I'll save this as diskhogs.sh, then use chmod +x diskhogs.sh to ensure it's executable. Now it's easier to figure out who are slackers in disk management:
$ ./diskhogs.sh
mitchell=13989MB
citarella=8299MB
Finally, let's wrap this in a for loop so we can actually generate that complaining e-mail from within the script. First, here's the general structure:
for result in $(du -s * | \
awk '$1 > 500000 { MB = $1 / 1024 ;print $2 "=" MB "MB" }')
do
account=$(echo $result | cut -d= -f1)
usage=$(echo $result | cut -d= -f2)
... now let's generate an e-mail for the disk hog:
done
Not too difficult a sequence. Notice the use of the cut command to pull the first and second field back out of the awk output once in the for loop.
The Final Script
Cleaning things up just a wee bit, here's the final script with a reasonably friendly e-mail message buried within:#!/bin/sh You are using more than your fair share of our disk space. All accounts on the system are allocated 500MB of space, but you're currently using $usage space. Please take whatever steps are necessary to reduce your disk usage ASAP, and let me know if you need any help migrating files or data to CD/DVD for archival purposes.
Dave Taylor
cd /home
for result in $(du -s * | \
awk '$1 > 500000 { MB = $1 / 1024 ;print $2 "=" MB "MB" }')
do
account=$(echo $result | cut -d= -f1)
usage=$(echo $result | cut -d= -f2)
# now let's generate an email for the disk hog
cat << EOF | mail -s "Exceeded disk allocation!" $account
To: $account
From: root
administrator
EOF
done
exit 0
Utilizing Cron to Automate the Script
The final step required is to have the script run every night without any further intervention, so you'll attain your goal of bugging disk hogs without any manual effort on your part. Fortunately, cron is a pretty straightforward utility to work with, though a quick glance at the crontab(5) man page is always helpful, where it reminds us that the format for entries is:
| field | allowed values |
| ----- | -------------- |
| minute | 0-59 |
| hour | 0-23 |
| day of month | 1-31 |
| month | 1-12 (or names, see below) |
| day of week | 0-7 (0 or 7 is Sun, or use names) |
To have the script run every weekday at 3:00 a.m. then, the fields should be:
0 3 * * 1,2,3,4,5
or, if you want to be a bit more fancy, you can use a range specifier:
0 3 * * 1-5
The last entry on the crontab line is the command to run and you do need to remember that crontab doesn't have a full PATH so you should always specify exactly where the script lives:
0 3 * * 1-5 /home/admin/diskhogs.sh
That's all there is to it. Use the crontab -e command (while logged in as root) to add that line to the root crontab file, and the next weekday morning at 3:00 a.m. mitchell and citarella should both receive e-mail reminders of their disk use problem.
Oh, one more thing. If this script doesn't do the trick, you might find that some of your users are tucking files into directories other than /home, or are assigning ownership of very large files to system accounts to avoid being penalized for owning them. In that case, you'd want to replace the du with a call to find, and you can learn more about that with the more sophisticated version of this script presented as #40, diskhogs, at the Wicked Cool Shell Scripts Web site, www.intuitive.com/wicked.
Published June 15, 2004 Reads 12,766
Copyright © 2004 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Dave Taylor
Dave Taylor, a contributing editor to Linux.SYS-CON.com, has been involved with the Linux and Unix community since 1980 and has written a number of best-selling Unix books. Currently, he writes, teaches, and works as a management consultant to tech startups, along with his new venture, Ask Dave Taylor!, www.askdavetaylor.com and his personal blog is www.blog.Linux.SYS-CON.com. To contact Dave, please go to /www.intuitive.com/contact.shtml.
![]() |
questionsquad 09/06/07 03:16:03 AM EDT | |||
good job. keep it up. |
||||
- Kindle 2 vs Nook
- Is Cloud Computing Like Teenage Sex?
- GovIT Expo Highlights Cloud Computing
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Cloud Computing Can Revitalize Your Career as Software Developer
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Yahoo! SVP Shelton Shugar to Discuss Innovation at Cloud Computing Expo
- Virtualization Journal "Readers' Choice Awards" Voting Is Now Open
- Einstein, Sharks and Clouds: IT Security in the Cloud
- Adobe Flex Developer Earns $100K in New York City
- Virtualization Expo Call for Papers Deadline December 15
- Amazon Web Services Database in the Cloud
- Kindle 2 vs Nook
- Cloud CEOs, CTOs & SVPs to Speak at 4th International Cloud Computing Expo
- Is Cloud Computing Like Teenage Sex?
- 1st Annual GovIT Expo: Letter from the Technical Chair
- Ulitzer News: Search vs New Media
- The Difference Between Web Hosting and Cloud Computing
- Cloud Computing Expo: Exclusive Q&A with Yahoo! SVP Cloud Computing
- Confessions of a Ulitzer Addict
- GovIT Expo Highlights Cloud Computing
- Twitter, Linked In, Ning and Ulitzer: Easy Personal Branding Strategy
- My Thoughts on Ulitzer
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- The i-Technology Right Stuff
- Linux.SYS-CON.com Exclusive: Linus Discloses *Real* Fathers of Linux
- After Ubuntu, Windows Looks Increasingly Bad, Increasingly Archaic, Increasingly Unfriendly
- Linus' Top Ten SCO Barbs
- A Closer Look at Damn Small Linux
- Netscape Co-Founder's 12 Reasons for Growth of Open Source
- Introducing "Cooperative Linux" - Linux for Windows, No Less
- *POINT - COUNTERPOINT SPECIAL* What's Wrong with the Open Source Community?
- Where Are RIA Technologies Headed in 2008?
- Linux.SYS-CON.com Exclusive: What Would UserLinux Look Like?
- i-Technology Viewpoint: The New Paradigm of IT Buying
- Is Linux Desktop-Ready Yet...or Not?
































