Issues with cronjob : Script using sendmail


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Issues with cronjob : Script using sendmail
# 1  
Old 06-04-2009
Issues with cronjob : Script using sendmail

Hi All,

I am new to unix. I have created a cron job, that sends mail using sendmail utility. Am facing a unique problem while making a cron job for this script.

In the script I append a file to my mail using 'cat' command.

[code] cat $report >> $mailMsg [code]

& this $mailMsg is used as mail content(along with other static data) .

My problem is whenever I run the shell script manually everything works fine, the mail is sent successfully with my file($report) contents. But when a cron job executes the script, the mail is sent but the mail does not include the file contents($report).

Sample mail when script is executed manually executed:

========= Log Report =============
{$report contents}
===============================================================================
== Starting Test at: Jun 4, 2009 12:04:15 AM EDT

But from cron job I dont get report contents. Smilie

I only get 1st static line in mail.

========= Log Report =============

{no report file contents}

Can anyone tell me why is it happening?

Please help.

Thanks in Advance,
Anshul
# 2  
Old 06-04-2009
How is the cron deamon supposed to know where to look for the report file? You're not specifying any file path (?).
# 3  
Old 06-04-2009
Well file contents are part of the mail body, so I dont think I have cron deamon needs any info like that.

Please find below the code am using to perform this activity

Shell Script Code
----------------

emailText=/tmp/msgTxt.log
echo "" > $emailText
echo "Log Report" >> $emailText

echo "" >> $emailText
cat `ls -1tr /home/testadmin/testdir/test.log* | tail -1` >> $emailText

RECIPIENTS=testadmin@info.com

SUBJECT="Log Report Generated on "`date +%d-%m-%Y`"
MAILFILE=$emailText

echo "Sending email..."

echo To: $RECIPIENTS > yy
echo From: "$machineName" >> yy
echo Subject: $SUBJECT >> yy
echo MIME-Version: 1.0 >> yy
echo "Content-Type: multipart/mixed; boundary=MC" >> yy
echo --MC>> yy
echo "Content-Transfer-Encoding: 7bit" >> yy
echo "Content-Type: text/plain; charset=ISO-8859-1" >> yy
cat $MAILFILE >> yy
/usr/lib/sendmail $RECIPIENTS < yy
rm yy

rm $emailText

echo "Done!"
exit 0

This is want is been done in the shell script.

When the script is executed manually I am getting corret mail with mail body containing the "test.log" contents. BUt when executed by a cron job, the email body is missing the test.log contents. Smilie

Please advice were am I wrong.

Thanks & Regards,
Anshul
# 4  
Old 06-04-2009
O.k., second guess, then: replace cat with /bin/cat (or whatever which cat returns on your system) and try again ...

The point I'm driving at: cron does not "live" in the same environment your user does, which (i.a.) means, that what works from a "user shell" does not necessarily (or in a different way) work from a "cron shell".
# 5  
Old 06-04-2009
Thank you for prompt reply. I tried using /bin/cat in my script as suggested by you. But no luck Smilie

Am really not able to understand what happening in cron.
# 6  
Old 06-04-2009
It's getting more and more interesting Smilie

What are the permissions set for the log files and log folder, respectively? (Is the cron daemon allowed to read / access them?)
# 7  
Old 06-04-2009
Hi,

First of all I notice an unmatched " in:
<code>SUBJECT="Log Report Generated on "`date +%d-%m-%Y`"</code>

Secondly you make use of the file yy and it can be that the cron environment does not like that.
Better to do
<code>yy=/tmp/yy.tmp</code>

and

<code> >> $yy</code>

Although on my system it worked after removing the ".

And perhaps you can share the cron entry with us.

Greetings,

E.J.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Sendmail with HTML body and attachment code issues

Hi, I was working on getting an HTML file in the mail body along with attaching a "csv" file to the mail. Below are the 2 parts of the code. I need help with the second part where I'm sending the mail. The HTML file as an attachment is perfect without any issues and with proper formatting.... (6 Replies)
Discussion started by: aster007
6 Replies

2. UNIX for Dummies Questions & Answers

Sendmail issues

Hi, I'm having some problems with sendmail in Linux. The thing is when I type the following command to send an e-mail: sendmail bartoszk@companydomain.pl< test.txt it looks like it executed but I don't receive the mail. The file test.txt looks like that: To:... (3 Replies)
Discussion started by: bartoszk
3 Replies

3. UNIX for Dummies Questions & Answers

Having some issues with cronjob

Hi, i have a cronjob scheduled to run daily at 7AM , the job which i am executing has three other scripts in it to execute. The first scripts gets executed perfectly but the 2nd and 3rd scripts which take 4-5 mins to complete does not executes,is there anything that i am missing. This is... (3 Replies)
Discussion started by: vikatakavi
3 Replies

4. Shell Programming and Scripting

Shell script with cronjob

Hello All, I have a script which will cd into a directory based on current timestamp. It will then do a grep and search for a condition. It's run on a cron job that runs every 30 minutes. So if it finds a match in 00, it sends out the contents of that grep to an alert and emails out. Now... (3 Replies)
Discussion started by: liketheshell
3 Replies

5. UNIX for Dummies Questions & Answers

nLocal sendmail issues when hostname not set in official domain name

Hi, I'm having issues with sendmail when I try to send host to host mail. I've had to change the "my official domain name" line to mycompany.com to get the mails through the external spam filter, when a mail was sent with hostname.mycompany.com it was blocked. I had to change the Dj macro... (0 Replies)
Discussion started by: elcounto
0 Replies

6. UNIX for Dummies Questions & Answers

Facing issues while running a cronjob !

Hi, I am trying to run a cronjob. But while doing so I am getting the following error message :- can't open yourfile in /var/spool/cron/crontabs directory. No such file or directory How can I resolve this issue ? Please help. Thanks Please view this code tag video for... (14 Replies)
Discussion started by: acidburn_007
14 Replies

7. UNIX for Dummies Questions & Answers

Script not running through Cronjob

Hi, I have a .ksh script which updates the database. The script is running fine manually but it is not running through cron.All the file permissions are fine. The script contents are as below: #!/usr/bin/ksh ddate=`date +%Y%m%d` echo $ddate nohup sqlplus crm/crm @db_state_sync.sql >>... (3 Replies)
Discussion started by: shivangi
3 Replies

8. UNIX for Dummies Questions & Answers

how to cancel a cronjob if the cronjob still running

hi everyone I'm newbie in this forum hope I can get some help here :) I have a command in crontab that executed every 1 minute sometime this command need more than 1 minute to finish the problem is, the crontab execute this command although it's not finish processing yet and causing the system... (7 Replies)
Discussion started by: 2j4h
7 Replies

9. Solaris

Creating a CronJob Script

Hi All, Good Day. Anyone can guide me on how to create a cronjob script that run prstat -a and vmstat 10 10 on the certain time of the day and produce a log which will be kept in a specific directory? I am running on Solaris 10. Thanks. (2 Replies)
Discussion started by: ronny_nch
2 Replies

10. Shell Programming and Scripting

cronjob inside the script

Hiii, can nayone provide me the sample script which can do the following tasks--- I will apperciate if anyone give me their valuable inputs.I guess this is not at all a big task for the experts in the forum so kindly help me out in this. (3 Replies)
Discussion started by: namishtiwari
3 Replies
Login or Register to Ask a Question