Solved

Send output from bash script to mail

Posted on 2008-10-27
9
2,408 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
[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
  • 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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
 
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

626 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