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

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
deliriumxxAsked:
Who is Participating?
 
steelseth12Connect With a Mentor Commented:
Maybe DOCUMENT_ROOT is not available on your server.

try

$dirname = dirname($_SERVER['SCRIPT_FILENAME'])."/mms/data/";
0
 
mankowitzCommented:
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
 
deliriumxxAuthor Commented:
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
deliriumxxAuthor Commented:
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
 
steelseth12Commented:
Are you using
$dir = $_SERVER['DOCUMENT_ROOT']."/mms/data/";

or

$dirname = "../data";

??
0
 
crustymonkeyCommented:
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
 
deliriumxxAuthor Commented:
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
 
deliriumxxAuthor Commented:
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
 
deliriumxxAuthor Commented:
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
 
deliriumxxAuthor Commented:
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
 
deliriumxxAuthor Commented:
I closed the bracket on

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

lol
0
 
deliriumxxAuthor Commented:
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
 
steelseth12Commented:
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
 
deliriumxxAuthor Commented:
I will,,, thank you,,,,

I will post back here if I figure it out somehow
0
 
crustymonkeyCommented:
DOCUMENT_ROOT is set by Apache/IIS.  It's not relevant to something you are running from the cli (or cron).
0
 
deliriumxxAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.