Solved

Send output from bash script to mail

Posted on 2008-10-27
9
2,371 Views
Last Modified: 2013-12-26
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
Comment
Question by:Julian Matz
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 14

Expert Comment

by:Deepak Kosaraju
ID: 22818040
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
 
LVL 48

Expert Comment

by:Tintin
ID: 22818056
By default cron will mail any output to the cronjob owner.
0
 
LVL 21

Author Comment

by:Julian Matz
ID: 22818126
@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
 
LVL 12

Expert Comment

by:hfraser
ID: 22818155
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 14

Expert Comment

by:Deepak Kosaraju
ID: 22818196
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
 
LVL 48

Expert Comment

by:Tintin
ID: 22818262
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
 
LVL 14

Expert Comment

by:Deepak Kosaraju
ID: 22818358
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
 
LVL 48

Accepted Solution

by:
Tintin earned 500 total points
ID: 22818418
$$ 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
 
LVL 21

Author Comment

by:Julian Matz
ID: 22818428
Thanks, Tintin, that's exactly what I wanted to know.
-Julian
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now