Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-22-2014, 06:14 AM   #1
LQ Newbie
Registered: Sep 2014
Posts: 5

Rep: Reputation: Disabled
How to send e-mail from cron only in special circumstances

I wrote very simple bash script, which runs every hour and just restart some service. No every time I receive output of those commands through e-mail. I would like to receive it only in case of errors or something unusual.

The first idea was to run commands twice and compare for differences (but it's to much work, it can differ slightly). The second was to redirect output to /dev/null, and send through e-mail only error stream (how to do it?). The third is to check result (ok - result==0), and mail only when result is other than that.

Any ideas?
Old 09-22-2014, 07:26 AM   #2
LQ Newbie
Registered: Mar 2010
Location: India
Distribution: Centos
Posts: 28

Rep: Reputation: 2


I hope this will help you.after running any command,get the status of the command ran(success or fail).

for example:
service http restart

#### now check the status using if condition,if it not ran successfully mail to the user ##
if [ "$exit_status" -ne 0 ]
echo "command not successfull"
mail -s "http restart info"
echo "command was success"
Old 09-22-2014, 09:26 AM   #3
LQ Newbie
Registered: Sep 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Cron automatically send output of command to me, therefore it's useless to use `mail`.
But your post was inspiration for me, so I made script:

output2=$(/usr/sbin/service nginx reload)
if [ "$exit_status" -ne 0 ]; then
echo "error in reload, check it"
echo $output
echo $output1
echo $output2
I need to test it, but I hope I'm close to achieve my goal.
If you have some remarks, write it!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
using cron to backup logs in unusual circumstances ShadowWizard Linux - Newbie 4 04-30-2012 07:30 AM
Really simple cron to send an email every week to confirm mail is working stardotstar Linux - Newbie 3 03-01-2012 07:55 PM
[SOLVED] CentOS - Postfix - cron tasks don't send mail sburnay Linux - Distributions 4 09-12-2011 09:45 AM
Postfix : mail cannot send to send outside ( can send/receive locally) bobbinsupport Linux - Networking 3 12-15-2007 10:40 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:57 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration