LinuxQuestions.org
Visit Jeremy's Blog.
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 10-19-2011, 04:25 PM   #1
sukinder
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Rep: Reputation: Disabled
Question Using mailx in if statement


Hi,

I am totally new to shell scripts. I have a requirement to send mails if a job fails. I wrote the below script and setup the cron job. However, the scripts sends mail even if the job is successful. Could anyone please help me to resolve this issue:

My script:

#!/bin/bash
cd /interface/i_tane01/

mv /PROD/incoming/ABC.txt /PROD/archive/ABC`date '+_%m%d%y_%H:%M:%S'`.txt

if [ "$?" -ne "0" ];
then echo "Archeiving Error" | mailx -s "ABC_Err" ABC@123.com
exit 1
fi

When I execute the same script manually using ./file.sh command, it works very well. But, when I set this in cron job, it is sending mails everytime.

Your help on this is very much appriciated.

Thanks,
 
Old 10-19-2011, 05:03 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by sukinder View Post
the scripts sends mail even if the job is successful.
From 'man 5 crontab': "If MAILTO is defined (and non-empty), mail is sent to the user so named. If MAILTO is defined but empty (MAILTO=""), no mail will be sent.".
 
Old 10-20-2011, 04:37 PM   #3
sukinder
LQ Newbie
 
Registered: Oct 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thank you for your response.

But I am not sure how to check this as a thrid party is involved in setting up the cron jobs. Could you please elobrate on this?
Many thanks in advance!
 
Old 10-20-2011, 05:24 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by sukinder View Post
I am not sure how to check this
grep the cronjob, just a plain text file, for the variable named "MAILTO" ('grep -i ^mailto= /path/to/file') or open it using your favorite text editor.
 
  


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
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement Aquarius_Girl Programming 4 06-28-2010 03:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM
mailx pramka SUSE / openSUSE 3 05-01-2008 04:18 PM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM

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

All times are GMT -5. The time now is 01:29 AM.

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