LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2012, 01:51 PM   #1
jevaba
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Rep: Reputation: Disabled
PHP Pear Mail works from CLI but not from cron


I have a shell script that calls a php script to send an email. Both the shell script and php script work indenpendantly and together when manually envoked, and I get the email. However, when I run either from cron, I do not get the email. According to the log file output from the cron job, the script is running, just never sends the email. Can't figure this one out.

Shell Script:

Code:
#!/bin/bash
. ${HOME}/.bash_profile


echo "Starting...." `date`

LOGFILE1=/tmp/mvpn_status_1.log
LOGFILE2=/tmp/mvpn_status_2.log

echo "Temp files created" `date`

/sbin/ip tunnel > $LOGFILE2
echo "                Date/Time : "`date` > $LOGFILE1
echo " " >> $LOGFILE1
echo "       " $HOSTNAME "up time : "`/usr/bin/top -b -n 1|grep 'top - '|awk '{print $5}'`"(hrs:mins)" >> $LOGFILE1
echo " " >> $LOGFILE1
echo "     Tunnels on" $HOSTNAME ": "`wc -l $LOGFILE2|awk '{print $1}'` >> $LOGFILE1
echo " " >> $LOGFILE1
echo "   CPU Usage on" $HOSTNAME ": "`/usr/bin/top -b -n 1|grep 'Cpu(s)'` >> $LOGFILE1
echo " " >> $LOGFILE1
echo " Memory Info on" $HOSTNAME ":" >> $LOGFILE1
cat /proc/meminfo >> $LOGFILE1
echo " " >> $LOGFILE1
echo "  Disk Space on" $HOSTNAME ":" >> $LOGFILE1
df -lkH >> $LOGFILE1

echo "Log file built" `date`

echo "Executing email script..." `date`

/opt/lampp/bin/php /root/mail_status.php

echo "Email script done" `date`

echo "Cleaning up..." `date`

rm -f $LOGFILE1
rm -f $LOGFILE2

echo "All Done!" `date`
PHP Script:

Code:
#!/opt/lampp/bin/php -q

<?php
include("/opt/lampp/lib/php/Mail.php");
/* mail setup recipients, subject etc */
$recipients = "someone@somewhere.com";
$headers["From"] = $_ENV["HOSTNAME"] . "@somewhere.com";
$headers["To"] = "someone@somewhere.com";
$headers["Subject"] = $_ENV["HOSTNAME"] . " Status";
$mailmsg = file_get_contents("/tmp/mvpn_status_1.log");
/* SMTP server name, port, user/passwd */
$smtpinfo["host"] = "IP ADDRESS OF EMAIL SERVER";
$smtpinfo["port"] = "PORT FOR EMAIL SERVICE";
/* Create the mail object using the Mail::factory method */
$mail_object =& Mail::factory("smtp", $smtpinfo);
/* Ok send mail */
$mail_object->send($recipients, $headers, $mailmsg);
?>
Cron Entry:

Code:
16 13 * * * . ${HOME}/.bash_profile; /root/status-job2.sh > /var/log/status-job2.log 2>&1
Anyone got any ideas?
 
Old 02-28-2012, 03:09 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
Maybe env variables problem ?

man crontab
Code:
...
 Nor does it do any special environment handling.   A  shell  script  is
 better-suited  to doing that than a cron daemon.  This cron daemon sets
 up only three environment variables: USER, HOME, and SHELL.
...
 
Old 02-28-2012, 03:50 PM   #3
jevaba
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Agreed, but I'm executing the .bash_profile file which should set up all the variables for that user (in this case root):

Code:
${HOME}/.bash_profile
 
Old 02-28-2012, 05:50 PM   #4
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
Is HOSTNAME set in /root/.bash_profile ?
 
Old 02-28-2012, 09:47 PM   #5
jevaba
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks for your replies, I'm usually pretty good and figuring these things out, but this one has me a bit stumped. Not sure, but seems if that were an issue, it would be reporting that in the log file. For instance, when I have cron run the php script directly (without the shell script calling it), it logs an error at $mailmsg = file_get_contents("/tmp/mvpn_status_1.log"); since that file is being created in the shell script. Maybe I can write a script that outputs all the environment variables and run that from cron and see what is/isn't there.
 
Old 02-28-2012, 09:59 PM   #6
jevaba
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Yes, HOSTNAME is available.
 
  


Reply


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
PHP Pear Mail Packege - support security through SSL ? fatrandy13 Programming 3 08-23-2011 07:38 AM
install Pear Net/SMTP, Pear Log and Pear DB modules cccc Debian 0 02-26-2007 08:33 AM
php mail() fails from apache but not from CLI matiasquestions Linux - Server 0 11-24-2006 11:04 AM
php script will not run correctly as cron, fine from cli dtra Linux - Software 2 12-22-2005 07:21 AM
cron issue, php script runs fine in browser and cli dtra Linux - Software 1 07-15-2005 05:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:35 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration