Solved

Send output from bash script to mail

Posted on 2008-10-27
9
2,380 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

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…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

770 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