Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Did you put a 'l' (as in Lamda) for the argument after fdisk?
If that's not it, post the output of:
bash$ fdisk -l | cut -c1-9 | grep /dev/
I'm thinking that this method might not be very portable....
p.s.- Note my reply to acid_kewpie.... it turns out that 'paste' wanted a file... but we don't need paste if we put it all in an echo containing `date`.
Here is the output from the command you suggested:
-------------------------------------------------------------------------
[root@xxxx]# bash$ /sbin/fdisk -l | cut -c1-9 | grep /dev/
bash: bash$: command not found
--------------------------------------------------------------------------
Note that I get the same output from the server that your script works on.
Also, what works for me is '/sbin/fdisk -l'on my one server (that is where fdisk is located (redhat 7.2). In your script you have 'fdisk -l'.
With the changes from yourelf and acid_kewpie would this be the final script (in my case):
-----------------------------------------------------------------------------------
#!/bin/bash
for i in $( /sbin/fdisk -l | cut -c1-9 | grep /dev/ ) # Get all partitions
do
if [ "$( df -h $i | grep -v U | cut -c40-42 )" -ge "80" ]; then
p.s.- I didn't mean for you to put the "bash$" in there
Also, you don't need the 'date > /tmp/date_output.txt' line in there anymore (it's not used because of the alterations mentioned.)
edit:
If you want to see what is going on in here, single out the commands. Start with the first, then add a pipe, then the next, and so on. ex (I'm not showing the output):
Oops my bad. The script works on the server where all partitions are /dev/hda. On the server with 5 /dev/hdc partitions and 1 /dev/sda partition this is the output i get when i run your command.
Usage: fdisk [-l] [-b SSZ] [-u] device
E.g.: fdisk /dev/hda (for the first IDE disk)
or: fdisk /dev/sdc (for the third SCSI disk)
or: fdisk /dev/eda (for the first PS/2 ESDI drive)
or: fdisk /dev/rd/c0d0 or: fdisk /dev/ida/c0d0 (for RAID devices)
...
------------------------------------------------------------------------------------
And this would be the good copy of the script:
-----------------------------------------------------------------------------------
#!/bin/bash
for i in $( /sbin/fdisk -l | cut -c1-9 | grep /dev/ ) # Get all partitions
do
if [ "$( df -h $i | grep -v U | cut -c40-42 )" -ge "80" ]; then
yeah some versions of fdisk insist on being told which device to list, some default to all.... not sure if it's newer or older versions that do this...
Well, I've got the script on 3 servers now and all produce output to /var/log/disk_free.log every 60 minutes which is great. The problem now is that i setup cron to email me if disk space is over a certain percentage once it starts the script every 60 minutes. However the emails aren't getting to me (one of the servers is over its threshold so i should get something). Don't know if you know much about cron. But thanks for the help on the disk space script!!! Here is my crontab entry:
------------------------------------------------------------------------------
#check disk space every 60 minutes
MAILTO=me@mycompany.com
00 * * * * /usr/local/bin/disk_space.sh
------------------------------------------------------------------------------
I was receving the emails yesterday telling me i had my syntax for the /sbin/fdisk line incomplete. Any suggestions?
You can use the script to notify you. I don't really know the format that you want in the e-mails, but here's an example to work from:
below the ">> var/log/disk_free.log" line in the script:
Code:
tail /var/log/disk_free.log | mail -s "DISK USAGE ALERT ON `hostname -f`" your@address
The only thing about this is that it will send you 1 email for every single partition that is over 80% used (every hour). Maybe if I get some time I'll post a better solution.
EDIT:
What do you mean you were getting e-mails saying that the fdisk command was incomplete? What did they say? I can try to help if you tell me what is going wrong.
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,803
Rep:
Quote:
Originally posted by PTBmilo
The only thing about this is that it will send you 1 email for every single partition that is over 80% used (every hour). Maybe if I get some time I'll post a better solution.
In cases like that, I use something like:
Code:
#!/bin/bash
REPORT=/tmp/df_report_$$.txt
THRESH=80
TRUE="1 eq 1"
FALSE="1 eq 0"
WROTETOREPORT=$FALSE
df -k | grep -v "Mounted on" | while read DFREC
do
SPECIAL=`echo $DFREC | awk '{ print $1 }'`
FS=`echo $DFREC | awk '{ print $6 }'`
USE=`echo $DFREC | awk '{ print $5 }'`
PERCENT=`echo $USE | sed 's/%//'`
if [ $PERCENT -gt $THRESH ]
then
WROTETOREPORT=$TRUE
echo "$SPECIAL ($FS) is at $PERCENT percent." >> $REPORT
fi
done
if [ $WROTETOREPORT ]
then
cat $REPORT | mutt -s "Filesystems above $THRESH percent on date +'%Y-%m-%d %H:%M'" root@localhost
rm -f $REPORT
fi
Or something like that. It looks wierd at first but I use the variables (which are normally sourced, loaded as a module, or included as a header) to hold the boolean values so I don't have to remember what's true/false in shell, what's true/false in perl, etc.
Thanks again for all of your responses and most recently your one line solution to my emailing problem.....it's no longer a problem. All 3 servers are doing what they should be now.
----------------------------------------------------------------------------------
EDIT:
What do you mean you were getting e-mails saying that the fdisk command was incomplete? What did they say? I can try to help if you tell me what is going wrong.
-----------------------------------------------------------------------------------
No longer an issue.....thanks anyways.
Thanks to you to rnturn for the script you provided and everyone else for their help along the way. Hopefully this issue is now a closed case (at least by me).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.