LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-14-2008, 12:22 AM   #1
knewtolinux
LQ Newbie
 
Registered: Feb 2008
Posts: 6

Rep: Reputation: 0
Help with Script and Cron Job to e-mail df - h


I need to create a cronjob which will check disk space an e-mail the result to me.

Could someone help me? I have been looking high and low and tried several different finds from Google but best I can get is a script to run but never e-mails me so assuming I have something wrong. Once successful then need to setup a daily cronjob to email df -h results.

I also man cronjob and still having problms. I thought I had this down but I'm wrong.

Any help is greatly appreciate.

I'm running:
Red Hat Enterprise Linux

Here's the closet I have that doesn't create errors but never send e-mail.


#!/bin/sh
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge 1 ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
mail -s "Disk Check $usep%" me@me.com
fi
done
 
Old 02-14-2008, 01:24 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try
set -xv
at the top of your script and run by hand. It'll show you what's really happening.
For one thing, $usep and $uesp% are 2 diff vars(!). Try ${usep}%
 
Old 02-14-2008, 01:32 AM   #3
yuiy88
LQ Newbie
 
Registered: Feb 2007
Distribution: Fedora&Debian
Posts: 10

Rep: Reputation: 0
Put the script at /etc/cron.daily/

Then, edit crontab
-> vi /etc/crontab
-> input MAILTO=root
-> save the file

Or

Create a file at /etc/cron.d/check-space
-> vi check-space
-> input the following
MAILTO=root
10 1 * * * root /etc/cron.daily/script
-> save the file

Try this...Hope can help
 
Old 02-14-2008, 01:54 AM   #4
knewtolinux
LQ Newbie
 
Registered: Feb 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Looked quickly and have MailTo=root

Here's my crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
~
~
~
~
~
~
~
~
~
~
~
~
~
"/etc/crontab" 10L, 255C



Going to see what set -xv and chaging the var. Should I be looking at something with MTA/mail server as well?
 
Old 02-14-2008, 02:01 AM   #5
yuiy88
LQ Newbie
 
Registered: Feb 2007
Distribution: Fedora&Debian
Posts: 10

Rep: Reputation: 0
For your case, if you just want to know the disk space daily, i suggest installing logwatch would be helpful.

Logwatch provides server information daily to root.

Please make sure your server can send mail to yourself.
 
Old 02-14-2008, 02:03 AM   #6
knewtolinux
LQ Newbie
 
Registered: Feb 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Here's the details ... Thanks all for helping me!
# ./df.sh
df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
echo $output
usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $2 }' )
if [ $usep -ge 1 ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
mail -s "Disk Check $usep%" me@me.com
fi
done
+ df -H
+ grep -vE '^Filesystem|tmpfs|cdrom'
+ awk '{ print $5 " " $1 }'
+ read output
+ echo 60% /dev/sda3
60% /dev/sda3
echo $output | awk '{ print $1}' | cut -d'%' -f1
++ echo 60% /dev/sda3
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=60
echo $output | awk '{ print $2 }'
++ echo 60% /dev/sda3
++ awk '{ print $2 }'
+ partition=/dev/sda3
+ '[' 60 -ge 1 ']'
+ mail -s 'Disk Check 60%' me@me.com
hostname
++ hostname
date
++ date
+ echo 'Running out of space "/dev/sda3 (60%)" on me.net as on Thu Feb 14 02:55:59 EST 2008'
+ read output
+ echo 16% /dev/sda1
16% /dev/sda1
echo $output | awk '{ print $1}' | cut -d'%' -f1
++ echo 16% /dev/sda1
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=16
echo $output | awk '{ print $2 }'
++ echo 16% /dev/sda1
++ awk '{ print $2 }'
+ partition=/dev/sda1
+ '[' 16 -ge 1 ']'
+ mail -s 'Disk Check 16%' me@me.com
hostname
++ hostname
date
++ date
+ echo 'Running out of space "/dev/sda1 (16%)" on me.net as on Thu Feb 14 02:55:59 EST 2008'
+ read output
+ echo 100% /dev/sda5
100% /dev/sda5
echo $output | awk '{ print $1}' | cut -d'%' -f1
++ echo 100% /dev/sda5
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=100
echo $output | awk '{ print $2 }'
++ echo 100% /dev/sda5
++ awk '{ print $2 }'
+ partition=/dev/sda5
+ '[' 100 -ge 1 ']'
hostname
++ hostname
+ mail -s 'Disk Check 100%' me@me.com
date
++ date
+ echo 'Running out of space "/dev/sda5 (100%)" on me.net as on Thu Feb 14 02:55:59 EST 2008'
+ read output
+ echo 0% none
0% none
echo $output | awk '{ print $1}' | cut -d'%' -f1
++ echo 0% none
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=0
echo $output | awk '{ print $2 }'
++ echo 0% none
++ awk '{ print $2 }'
+ partition=none
+ '[' 0 -ge 1 ']'
+ read output
+ echo 7% /dev/sdb1
7% /dev/sdb1
echo $output | awk '{ print $1}' | cut -d'%' -f1
++ echo 7% /dev/sdb1
++ awk '{ print $1}'
++ cut -d% -f1
+ usep=7
echo $output | awk '{ print $2 }'
++ echo 7% /dev/sdb1
++ awk '{ print $2 }'
+ partition=/dev/sdb1
+ '[' 7 -ge 1 ']'
hostname
++ hostname
date
++ date
+ echo 'Running out of space "/dev/sdb1 (7%)" on me.net as on Thu Feb 14 02:55:59 EST 2008'
+ mail -s 'Disk Check 7%' me@me.com
+ read output
 
Old 02-14-2008, 02:07 AM   #7
knewtolinux
LQ Newbie
 
Registered: Feb 2008
Posts: 6

Original Poster
Rep: Reputation: 0
How much is logwatch?

What would be the easiest way to see if the server can send mail to me.
 
Old 02-14-2008, 02:22 AM   #8
yuiy88
LQ Newbie
 
Registered: Feb 2007
Distribution: Fedora&Debian
Posts: 10

Rep: Reputation: 0
Logwatch is a free software tool in linux.

You can install it using the command below
-> yum install logwatch
 
Old 02-14-2008, 12:40 PM   #9
knewtolinux
LQ Newbie
 
Registered: Feb 2008
Posts: 6

Original Poster
Rep: Reputation: 0
When I run yum install logwatch it's not valid.

I tried I tried to run a simple test and watch the message...

I ran:

echo "testing" | mail -s "just a test" helpme@help.com





I see the following.....

Feb 14 09:29:45 server sendmail[21636]: m1EETjGj021636: to=helpme@help.com, ctladdr=root (0/0), delay=00:00:00,

xdelay=00:00:00, mailer=relay, pri=30054, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (m1EETjoS021638 Message

accepted for delivery)



Feb 14 09:30:45 server sendmail[21640]: m1EETjoS021638: to=<helpme@help.com>, ctladdr=<root@server.net> (0/0), delay=00:01:00, xdelay=00:01:00, mailer=esmtp, pri=30316, relay=help.com. [24.154.1.11], dsn=4.0.0, stat=Deferred: Connection timed out with help.com.
 
Old 02-14-2008, 06:48 PM   #10
knewtolinux
LQ Newbie
 
Registered: Feb 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Finally got it to work. Had to configure "Smart".

Thanks all!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
script does not run in cron job kashyapvirgo Linux - General 8 03-20-2007 10:55 AM
Bash script and cron job rust8y Linux - General 2 07-08-2006 07:45 AM
Cron Job with a Script bravored Linux - General 4 08-05-2005 10:27 AM
cron job for expect script ruchitadesai Programming 9 05-27-2005 04:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration