Solved

Send output from bash script to mail

Posted on 2008-10-27
9
2,368 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Utilizing an array to gracefully append to a list of EmailAddresses
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.

762 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

18 Experts available now in Live!

Get 1:1 Help Now