| By Dave Taylor | Article Rating: |
|
| June 15, 2004 12:00 AM EDT | Reads: |
13,511 |
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 13,511
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. |
||||
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Linux Virtualization and Tired Open Source Myths
- IGEL Supports Red Hat Enterprise Virtualization 3.0
- CloudLinux Announces Support for Atomia
- Amazon Kindle Fire Gets Its Own 'Personal Cloud Desktop' with AlwaysOnPC App Launch
- SPIRIT DSP Receives 2011 INTERNET TELEPHONY Product of the Year Award
- Hadoop Quickstart: Use Whirr to automate standup of your distributed cluster on Rackspace
- Jury Gets Novell Antitrust Case Against Microsoft
- The Utility Infrastructure Security Market 2012-2022: Cybersecurity & Smart Grids
- FORTUNE Magazine Names Rackspace Among “100 Best Companies to Work For”
- iFollowOffice Turns to Virtual Bridges and Savvis for On-Demand Virtual Desktop Services
- Convirture Reports Strong 2011 as Virtualization Management Takes Off
- i-Technology in 2012: Five Industry Predictions
- Ubuntu-based Open Source Linux Mint Tests KDE Version
- Amazon to Rent Out Supercomputers
- Amazon Émigré Starts Network Monitoring Firm
- HP’s Putting a Back Door in the Itanium Alamo
- Linux Virtualization and Tired Open Source Myths
- CloudLinux Announces Preferred Partner Program
- MapR Pushes the Hadoop Envelope
- Rightware Announces Gaming Performance Benchmark for OpenGL ES 3.0/Halti
- IGEL Supports Red Hat Enterprise Virtualization 3.0
- CloudLinux Announces Support for Atomia
- 3Dconnexion Announces its Newest 3D Mouse - the SpaceMouse Pro
- 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
- A Closer Look at Damn Small Linux
- Linus' Top Ten SCO Barbs
- SCO CEO Posts Open Letter to the Open Source Community
- Netscape Co-Founder's 12 Reasons for Growth of Open Source
- Where Are RIA Technologies Headed in 2008?
- *POINT - COUNTERPOINT SPECIAL* What's Wrong with the Open Source Community?
- Introducing "Cooperative Linux" - Linux for Windows, No Less
- Linux.SYS-CON.com Exclusive: What Would UserLinux Look Like?
- Why Recovering a Deleted Ext3 File Is Difficult . . .





















