LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

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


Hi!
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
nisagnel
LQ Newbie
 
Registered: Mar 2010
Location: India
Distribution: Centos
Posts: 28

Rep: Reputation: 2
Smile

Hi,

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
exit_status=$?

#### now check the status using if condition,if it not ran successfully mail to the user ##
if [ "$exit_status" -ne 0 ]
then
echo "command not successfull"
mail -s "http restart info" yourname@mail.com
else
echo "command was success"
fi
 
Old 09-22-2014, 09:26 AM   #3
memorum
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:

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:21 PM.

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