Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CRON JOB executes differently then when a file is triggered  MANUALY. WHY?

Posted on 2007-07-31
16
Medium Priority
?
199 Views
Last Modified: 2010-07-27
I am running a simple PHP script on my server that basically takes a file from a directory and sends it as email message to one of the email addresses that I have in a list in a TXT file.

The script works PERFECT when I trigger it manually,,, but when I schedule a CRON JOB, the script executes only partially.

The script is supposed to check if a folder containing TXT files that are sent as emails is empty. IF the directory is empty the admin gets the message that the directory is empty, however,,, when the directory is not empty it takes a file from the directory,,, sends it as email,, and removes the file from directory.

As I mentioned,,, this all happens as clockwork when I trigger the script manualy....

HOWEVER,,, when I schedule a cron job,, the cron is consistenlty reporting that the directory is empty, even when it is not empty.... What am I doing wrong,,,,

All help appreciated...

Here is what I use to check if the directory is empty:

//Code

$dir = $_SERVER['DOCUMENT_ROOT']."/mms/data/";
$isEmpty = (count(glob("$dir/*")) === 0) ? '0' : '1';

// it returns 0 when the directory is empty and returns 1 when it is not,, when I manually trigger the script
0
Comment
Question by:deliriumxx
  • 10
  • 3
  • 2
  • +1
16 Comments
 
LVL 24

Expert Comment

by:mankowitz
ID: 19601535
check to make sure that the cron is running with the right permissions. Make sure that your script has the right permissions and has the right default directories to start in.
0
 

Author Comment

by:deliriumxx
ID: 19601682
I have tried everything in regards to permissions. Folders and files permissions in all possible ways. is there anything specific I need to look into.

I am using CPanel Cron Job Scheduler,,, I have another Cron job for another script and that one works fine
0
 

Author Comment

by:deliriumxx
ID: 19603131
Here is the code for the directory being checked .... wether it is empty or not... As I said,,, It works when triggered manualy,,, when CRON does it,,,, it only reports that directory is empty, even when it is not empty.

// > > > > > > > Code
<?PHP

$dirname = "../data";

function is_emtpy_dir($dirname){
   if(is_dir($dirname) ){
       $handle = opendir($dirname);
       while( (gettype( $name = readdir($handle)) != "boolean")){
               $name_array[] = $name;
       }
       foreach($name_array as $temp)
           $folder_content .= $temp;

       closedir($handle);
       if($folder_content == "...") {
           return true;
       } else {
           return false;
       }
   }
   else {
       return true; // folder doesnt exist
         echo "Directory doesn't exist...";      
            }
}

      if (is_emtpy_dir($dirname) == false){

      mail('mymail@domain.com','Directory NOT Empty',$dirname." The directory is NOT empty!","From: somemail@domain.com\nX-Mailer: Signatures\n");
   
      echo "Dir is NOT Empty";

      }else if(is_emtpy_dir($dirname) == true) {

      mail('mymail@domain.com','Directory Empty',$dirname." The directory is empty!","From: somemail@domain.com\nX-Mailer: Signatures\n");

      echo "The directory is empty,,,";

      }
?>
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 20

Expert Comment

by:steelseth12
ID: 19603272
Are you using
$dir = $_SERVER['DOCUMENT_ROOT']."/mms/data/";

or

$dirname = "../data";

??
0
 
LVL 2

Expert Comment

by:crustymonkey
ID: 19603281
First thing I would do is use the full path to your directory instead of:

$dirname = "../data";

It should be something like:

$dirname = "/var/data";

or whatever the full path is to that data directory.  Anything that you run via cron should, as a general rule, always contain full paths.  The cwd that you are going to start out in varies depending on the cron daemon being used and it's settings, but more than likely, it will start you in your home directory.  Just remember, no relative paths.

One thing that I would definitely do is this.  Most cron daemons allow you to specify in your crontab environment variables.  One important one is the MAILTO var.  This allows you to specify where any output from a command is emailed to.  You should modify your crontab so that you receive an email to a valid email address if something goes wrong:

# Crontab
MAILTO=you@yourdomain.tld
0 0 * * *     /path/to/program arg1 arg2

What this will do is send an email to you@yourdomain.tld with any output from the execution of /path/to/program.  If you are experiencing an error or want to add some debugging output, this will show it to you.

Have fun
0
 

Author Comment

by:deliriumxx
ID: 19603378
I did this,,, so I am using a full path...

$dirname = $_SERVER['DOCUMENT_ROOT']."/mms/data/";

and I am getting emails from Cron with no errors,,, the cron email I get simply outputs an echo from the file that says "The directory is empty"
0
 

Author Comment

by:deliriumxx
ID: 19603394
The part that intrigues me is that in the output for the email I get this

/mms/data/ The directory is empty!

because I sent the email message to output

$dirname." The directory is empty!"

Is cron supposed to cut off the $_SERVER['DOCUMENT_ROOT'] part of the path?
0
 

Author Comment

by:deliriumxx
ID: 19603457
I have been changing around the methods to check if the directory is empty and the issue is obviously not in the function.... it works correctly when it is triggered in a browser,,, unfortunately I NEED a CRON...

Could the problem be the array that folder contents is stored into?
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 150 total points
ID: 19603469
Maybe DOCUMENT_ROOT is not available on your server.

try

$dirname = dirname($_SERVER['SCRIPT_FILENAME'])."/mms/data/";
0
 

Author Comment

by:deliriumxx
ID: 19603563
I tried the $dirname = dirname($_SERVER['SCRIPT_FILENAME'])."/mms/data/";

What  get now for output is
/mms The directory is empty!

The "/data/" portion of the path is gone,,,

But I still get that the directory is empty for mms as well,, which is additionally impossible

I am thinking,,,, are there Possible Command line Arguments that would change this Cron behaviour.

I know that -q ahoudl work though

OH,,, HERE is the CRON I am using (I CPANEL -> STANDARD)

/usr/local/bin/php -q /home/MYUSERNAME/public_html/mms/config/def.php

(def is the file containing the code)
0
 

Author Comment

by:deliriumxx
ID: 19603573
I closed the bracket on

$dirname = dirname($_SERVER['SCRIPT_FILENAME'])."/mms/data/");

lol
0
 

Author Comment

by:deliriumxx
ID: 19604031
You were right when you said "Maybe DOCUMENT_ROOT is not available on your server"

When I hard code the path the script seems to work.
Do you know of a way to deal with this cron setting?
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 19604136
You can try running it through curl

curl http://www.yoursite.com/script.php

see http://curl.haxx.se/docs/manpage.html for more details
0
 

Author Comment

by:deliriumxx
ID: 19605113
I will,,, thank you,,,,

I will post back here if I figure it out somehow
0
 
LVL 2

Expert Comment

by:crustymonkey
ID: 19605346
DOCUMENT_ROOT is set by Apache/IIS.  It's not relevant to something you are running from the cli (or cron).
0
 

Author Comment

by:deliriumxx
ID: 19606241
crustymonkey said: DOCUMENT_ROOT is set by Apache/IIS.  It's not relevant to something you are running from the cli (or cron).

Would you know how to address this issue though?

I don't want to hardcode the path to the directory I am working with every time I need it :S

0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question