• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2428
  • Last Modified:

Send output from bash script to mail

Hi!

I'm writing a simple bash script to place in /etc/cron.daily/. The script runs a couple of commands. What I'm wondering is how do I send all stdout and stderr to mail once the script has exited?
0
Julian Matz
Asked:
Julian Matz
  • 3
  • 3
  • 2
  • +1
1 Solution
 
Deepak KosarajuCommented:
schedule the script as follows under crond.daily
@daily <script name>  > /tmp/output 2>$1

@daily process.sh > /tmp/output 2>$1

in you script at the ending add mail process as
"cat |mail -s "" "
For Example:
I like to know number of process running with backup at midnight my process.sh contain
#!/bin/sh
ps -ef | grep -wi "backup.sh" | grep -v "grep"
d=`date +%D-%H:%S:`
`cat /tmp/output | mail -s "Backup Status on $d"  backupadmin@test.com`
(or)
`mail -s "Backup Status on $d" backupadmin@test.com < /tmp/output`


0
 
TintinCommented:
By default cron will mail any output to the cronjob owner.
0
 
Julian MatzAuthor Commented:
@kosarajudeepak: I could do this if I used crontab but what if place the script to be executed in the following directory:
/etc/cron.daily

@Tintin: I only seem to be getting stderr (which is good since my mailbox would be flooded otherwise).
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
Hugh FraserConsultantCommented:
You can send the output from any cron job to a user by adding

MAILTO="user@domain.com"

or disable it by using

MAILTO=""

as the first line in the cron file, but it's universal for all jobs in the cron file. Sometimes you don't want the output from all the jobs sent. In these cases, the re-direct works better.
0
 
Deepak KosarajuCommented:
Its simple redirect the command output to a file like first command as > and following command's as appending. so every time when script runs /tmp/output will be overwritten by the first command output and followed by appending status output of remaining commands.
#!/bin/sh
output="/tmp/output"
ps -ef | grep -wi "backup.sh" | grep -v "grep" > $output
ps -ef | grep -wi "status.sh" | grep -v "grep" >> $output
ps -ef | grep -wi "restore.sh" | grep -v "grep" >> $output
d=`date +%D-%H:%S:`
`cat /tmp/output | mail -s "Backup Status on $d"  backupadmin@test.com`
(or)
`mail -s "Backup Status on $d" backupadmin@test.com < /tmp/output`
0
 
TintinCommented:
The easiest way is to do
#!/bin/bash
exec 1>/tmp/$$ 2>&1
 
...
 
mail -s "Script output" some@user </tmp/$$

Open in new window

0
 
Deepak KosarajuCommented:
Tintin can you explain the following I am not expert in Shell Scripting I used $* $1$2 etc but I never used $$ what it implies and what dose
exce 1>/tmp/$$ does
#!/bin/bash
exec 1>/tmp/$$ 2>&1
 
...
 
mail -s "Script output" some@user </tmp/$$

Open in new window

0
 
TintinCommented:
$$ contains the process ID of the script and is commonly used to make up a temporary semi-unique filename.

The exec line ensures that all STDOUT and STDERR from the script gets redirected to /tmp/$$

Say you have the script

#!/bin/sh
date >/tmp/$$
ps -ef >>/tmp/$$
echo "Something" >>/tmp/$$
..

it's much easier to do

#!/bin/sh
exec 1>/tmp/$$
date
ps -ef
echo "Something"
0
 
Julian MatzAuthor Commented:
Thanks, Tintin, that's exactly what I wanted to know.
-Julian
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now