?
Solved

Shell script for send mail automatically

Posted on 2013-06-28
10
Medium Priority
?
450 Views
Last Modified: 2013-07-11
Hi Expert,

Need a shell script for send mail automatically when directory size is greater than 10 GB.  
I want to add that lines in below script...

#!/bin/bash
> /home/xerox/Desktop/userlist.csv
for i in Screenshot bind linux
do
du -h --max-depth=1 /home/xerox/Desktop/$i | sort -nr >> /home/xerox/Desktop/$
done

uuencode /home/xerox/Desktop/userlist.csv /home/xerox/Desktop/userlist.csv | mutt -s "Test email with attachments and content" -a /home/xerox/Desktop/userlist.csv -- xeroxzerox@ogy.in < /home/xerox/Desktop/blocksites.txt
 
Thanks,
0
Comment
Question by:xeroxzerox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 2
10 Comments
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 600 total points
ID: 39288472
I would run this as a cron job every hour (if you run it more often than this, you will be more likely to get several emails before you get a chance to reduce the size).

If you want to stick with "human" numbers for the "du" output, it makes life easier if you a non-human version for size checking.  I'm assuming that you only want to check the top-level folder for size - if any of the sub-folders are too big, the top one will also be too big!

I've corrected a couple of typos too - you need "sort -hr", and you'd missed off the name of the csv file in the loop.

The script becomes:
#!/bin/bash
> /home/xerox/Desktop/userlist.csv
rm -f .send_size_email
for i in Screenshot bind linux
do
  du -h --max-depth=1 /home/xerox/Desktop/$i | sort -hr >> /home/xerox/Desktop/userlist.csv
  if [ $(du /home/xerox/Desktop/$i | awk '{print $1}') -gt 10485760 ]; then
    touch .send_size_email
  fi
done

if [ -f .send_size_email ]; then
  uuencode /home/xerox/Desktop/userlist.csv /home/xerox/Desktop/userlist.csv | mutt -s "Test email with attachments and content" -a /home/xerox/Desktop/userlist.csv -- xeroxzerox@ogy.in < /home/xerox/Desktop/blocksites.txt
  rm -f .send_size_email
fi

Open in new window


Then add a line to your crontab (use "crontab -e" to laod the file in edit mode) and add:
    0 * * * * /path/to/email/send/script.sh
0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 39288650
I would replace uuencode /home/xerox/Desktop/userlist.csv /home/xerox/Desktop/userlist.csv with cat /home/xerox/Desktop/userlist.csv | uuencode /dev/stdout. The existing line has that effect, but only by happenstance: man uuencode says
BUGS
       If more than one file is given to uudecode and the -o option is given or more than one name in the encoded files are the same the result is probably not what is expected.
(The italics are mine - the rest is as displayed by man). You are sending an email with a uuencoded body and an attachment which is the original un-uuencoded  data - did you really want to do that?
To be continued (have to go now)
0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 39288781
Perhaps < /home/xerox/Desktop/blocksites.txt overrides uuencode ... | mutt ... as regards mutt's stdin. Still working on a more complete solution...
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 35

Expert Comment

by:Duncan Roe
ID: 39292446
Think I'm finally getting my head around this one. The initial scripts in the Q were more sophisticated than I realized: > /home/xerox/Desktop/userlist.csv neatly empties that file, for instance.
The du command you are using is going to print the overall size of Screenshot etc., so these are the sizes that will trigger mailing. http:#a39288472 line 7 needs to use du -s in the comparison though.
I'm at work, so I can't fully test this (no mutt)
!/bin/bash
#set -x   # Un-comment for debugging

# Parameterise stuff for shorter commands (and ease of future changes)
MY_DIR=/home/xerox/Desktop
USERLIST=userlist.csv
SIZ=10485760
#SIZ=10000   # For my testing

# Clean output file
> $MY_DIR/$USERLIST

for i in Screenshot bind linux
do
  [ $(du -s $MY_DIR/$i | awk '{print $1}') -gt $SIZ ] &&
    du -h --max-depth=1 $MY_DIR/$i | sort -nr >> $MY_DIR/$USERLIST
done

# Only send a mail if the output file is not empty
if [ -s $MY_DIR/$USERLIST ]; then
  TMP=$(tempfile)
  uuencode $MY_DIR/$USERLIST $MY_DIR/$USERLIST > $TMP
  mutt -s "Some directories are getting big" -a $TMP -- xeroxzerox@ogy.in < $MY_DIR/blocksites.txt
  rm $TMP
fi

Open in new window

I don't know why you want to send the attachment uuencoded, but i'm sure you know what you're doing.
0
 
LVL 19

Expert Comment

by:simon3270
ID: 39292948
Yes, duncan, you do need the "-s" on "du" to get the size of the whole directory - my bad.

The "du -h" line must either be:

   du --max-depth=1 $MY_DIR/$i | sort -nr

(to use "number of 1k blocks" in the output) or

   du -h --max-depth=1 $MY_DIR/$i | sort -hr

(to have sizes with k, M, G etc suffixes, then tell "sort" to use numbers like that).
0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 39294958
Yes I was testing on a rather old system where sort does not have a -h option. I knew I had to change it back on posting but time got tight and I forgot. Was going to fix it at first opportunity but that's now.
0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 39295039
This one works. You will have to switch the lines that are commented with those that are not, in the top section
08:30:36$ pwd
/home/dunc/tests
08:30:46$ vss|tail -6
3404    ./ee016
4480    ./ee094
5244    ./ee058
12232   ./ee097
16044   ./ee056
18476   ./ee071
08:30:49$ cat ee105.sh
#!/bin/bash
#set -x   # Un-comment for debugging

# Parameterise stuff for shorter commands (and ease of future changes)
#MY_DIR=/home/xerox/Desktop
MY_DIR=/home/dunc/tests
#USERLIST=userlist.csv
USERLIST=ee105.out
#SIZ=10485760
SIZ=10000   # For my testing
#FILES="Screenshot bind linux"
FILES="ee058 ee097 ee056"
#EMAIL=xeroxzerox@ogy.in
EMAIL=dunc@dimstar.local.net
#MSG=blocksites.txt
MSG=ee105.txt

# Clean output file
> $MY_DIR/$USERLIST

for i in $FILES
do
  [ $(du -s $MY_DIR/$i | awk '{print $1}') -gt $SIZ ] &&
    du -h --max-depth=1 $MY_DIR/$i | sort -hr >> $MY_DIR/$USERLIST
done

# Only send a mail if the output file is not empty
if [ -s $MY_DIR/$USERLIST ]; then
  TMP=$(tempfile)
  uuencode $MY_DIR/$USERLIST $MY_DIR/$USERLIST > $TMP
  mutt -s "Some directories are getting big" -a $TMP -- $EMAIL < $MY_DIR/$MSG
  rm $TMP
fi
You have new mail in /home/dunc/Maildir
08:30:57$ cat ee105.txt
This is a test
08:31:01$ ./ee105.sh 
08:31:06$ cat ee105.out
12M     /home/dunc/tests/ee097
16M     /home/dunc/tests/ee056
6.6M    /home/dunc/tests/ee056/faked
1.3M    /home/dunc/tests/ee056/files
128K    /home/dunc/tests/ee056/in_win
12K     /home/dunc/tests/ee056/togo

Open in new window

Now check my email
q:Quit  d:Del  u:Undel  s:Save  m:Mail  r:Reply  g:Group  ?:Help
  67 N   Jul 02 Experts Exchang (7.0K) Neglected Question Alert: RedHat REHL 6 - -bash:
  68 N   Jul 02 Experts Exchang (7.1K) Neglected Question Alert: loaded data form multip
  69 N   Jul 02 Experts Exchang (7.1K) Neglected Question Alert: DHCP clients get wrong
  70 N   Jul 02 Experts Exchang (7.1K) Neglected Question Alert: Outbound Route with Ela
  71 N L Jul 02 To MLUG         (4.6K) [MLUG] Golden ticket to be won for LCA 2014
  72 N   Jul 02 Experts Exchang (6.3K) A  Comment Added: Shell script for send mail auto
  73 N L Jul 02 To luv-main     (1.0K) what is new in kernel 3.10
  74 N   Jul 02 Experts Exchang (6.3K) A  Comment Added: Nagiosgraph Forbidden
  75 N + Jul 03 Duncan Roe      (0.5K) Some directories are getting big


---Mutt: ~/Maildir [Msgs:75 New:21 Old:1 7.7M]---(threads/date)-----------------(end)---

Open in new window

0
 
LVL 35

Expert Comment

by:Duncan Roe
ID: 39295579
Open item 75
i:Exit  -:PrevPg  <Space>:NextPg v:View Attachm.  d:Del  r:Reply  j:Next ?:Help
Date: Wed, 3 Jul 2013 08:31:05 +1000
From: Duncan Roe <duncan_roe@acslink.net.au>
To: dunc@dimstar.local.net
Subject: Some directories are getting big
User-Agent: Mutt/1.5.21 (2010-09-15)

[-- Attachment #1 --]
[-- Type: text/plain, Encoding: 7bit, Size: 0.1K --]

This is a test

[-- Attachment #2: file4XnPc6 --]
[-- Type: text/plain, Encoding: 7bit, Size: 0.3K --]

begin 644 /home/dunc/tests/ee105.out
M,3)-"2]H;VUE+V1U;F,O=&5S=',O964P.3<*,39-"2]H;VUE+V1U;F,O=&5S
M=',O964P-38*-BXV30DO:&]M92]D=6YC+W1E<W1S+V5E,#4V+V9A:V5D"C$N
M,TT)+VAO;64O9'5N8R]T97-T<R]E93`U-B]F:6QE<PHQ,CA+"2]H;VUE+V1U
M;F,O=&5S=',O964P-38O:6Y?=VEN"C$R2PDO:&]M92]D=6YC+W1E<W1S+V5E
),#4V+W1O9V\*
`
end


-N +- 75/75: Duncan Roe             Some directories are getting big          -- (all)

Open in new window

Type "v" to view attachments
q:Exit  s:Save  |:Pipe  p:Print  ?:Help
  I     1 <no description>                        [text/plain, 7bit, us-ascii, 0.1K]
  A     2 file4XnPc6                              [text/plain, 7bit, us-ascii, 0.3K]

Open in new window

Save the attachment. Verify original output then rename it. uudecode the attachment and verify same as original
08:24:05$ cat /home/dunc/tests/ee105.out
12M     /home/dunc/tests/ee097
16M     /home/dunc/tests/ee056
6.6M    /home/dunc/tests/ee056/faked
1.3M    /home/dunc/tests/ee056/files
128K    /home/dunc/tests/ee056/in_win
12K     /home/dunc/tests/ee056/togo
08:45:46$ bu /home/dunc/tests/ee105.out
'/home/dunc/tests/ee105.out' -> '/home/dunc/tests/ee105.out.bu'
08:45:52$ uudecode doc/file4XnPc6 
08:46:31$ cat /home/dunc/tests/ee105.out  
12M     /home/dunc/tests/ee097
16M     /home/dunc/tests/ee056
6.6M    /home/dunc/tests/ee056/faked
1.3M    /home/dunc/tests/ee056/files
128K    /home/dunc/tests/ee056/in_win
12K     /home/dunc/tests/ee056/togo
08:46:49$ cmbu /home/dunc/tests/ee105.out

Open in new window

You can get vss, bu, cmbu and a bunch more from https://github.com/duncan-roe/command_line_tools
0
 
LVL 35

Accepted Solution

by:
Duncan Roe earned 1400 total points
ID: 39319248
Did you extract the script and try it? Just to be clear, it should look like
#!/bin/bash
#set -x   # Un-comment for debugging

# Parameterise stuff for shorter commands (and ease of future changes)
MY_DIR=/home/xerox/Desktop
USERLIST=userlist.csv
SIZ=10485760
FILES="Screenshot bind linux"
EMAIL=xeroxzerox@ogy.in
MSG=blocksites.txt

# Clean output file
> $MY_DIR/$USERLIST

for i in $FILES
do
  [ $(du -s $MY_DIR/$i | awk '{print $1}') -gt $SIZ ] &&
    du -h --max-depth=1 $MY_DIR/$i | sort -hr >> $MY_DIR/$USERLIST
done

# Only send a mail if the output file is not empty
if [ -s $MY_DIR/$USERLIST ]; then
  TMP=$(tempfile)
  uuencode $MY_DIR/$USERLIST $MY_DIR/$USERLIST > $TMP
  mutt -s "Some directories are getting big" -a $TMP -- $EMAIL < $MY_DIR/$MSG
  rm $TMP
fi

Open in new window

0
 
LVL 6

Author Closing Comment

by:xeroxzerox
ID: 39319707
Thanks all of you
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question