Lunarpages Web Hosting Forum

Author Topic: Cron Issue Inquiry  (Read 7202 times)

Offline applejax

  • Newbie
  • *
  • Posts: 5
Cron Issue Inquiry
« on: December 03, 2009, 12:56:18 PM »
Hi, I set  up a cron job, but for some reason only part of the php script takes..

The script simply is supposed to copy a file from one folder to another and then send me a confirmation email saying it was completed; for some reason it skips the file copy and just shoots me the email. My cron command is below

php /home/USER/public_html/PATH/copy_email.php

Thanks in advanced for your help.

Offline MrPhil

  • Senior Moderator
  • Berserker Poster
  • *****
  • Posts: 6428
Re: Cron Issue Inquiry
« Reply #1 on: December 03, 2009, 02:54:13 PM »
So you know that the cron job itself is being run, but it's not doing quite the right thing. It probably doesn't matter, but I would use the -q flag to stop PHP from outputting page headers (php -q /home/USER...). Next, is your PHP code using PHP 5 features? If you're on a server that offers both PHP 4 and 5 to switch between, it will be using PHP 4 unless you explicitly give the path to the PHP 5 interpreter on the command line. The command line environment does not pay any attention to settings in .htaccess, such as using PHP 5. I don't know if it pays attention to php.ini, either. Speaking of which, make no assumptions about your environment settings, defined paths, or even what directory you start from.

If all that fails, could you post your copy_email.php here? XXXXX-out any sensitive information, such as your account name, site name, passwords, etc.

Note: this is based on my experiences with cPanel, but I would assume that cron under LPCP would work much the same way.
« Last Edit: December 03, 2009, 02:57:57 PM by MrPhil »
Visit My Site

E-mail Me
-= From the ashes shall rise a sooty tern =-

Offline applejax

  • Newbie
  • *
  • Posts: 5
Re: Cron Issue Inquiry
« Reply #2 on: December 03, 2009, 03:40:42 PM »
Hi MrPhil, thanks for your reply.

Yes I tried the -q as well (sorry. I should have mentioned that to begin with) It didn't make any difference though.

I saw the php path info in the wiki lunarpages area but I didn't get how that would apply to my script..

Anyway, here is the code I am using.

Code: [Select]
<?php
function copyFile($url,$dirname){
    @
$file fopen ($url"rb");
    if (!
$file) {
        return 
false;
    }else {
        
$filename basename($url);
        
$fc fopen($dirname."$filename""wb");
        while (!
feof ($file)) {
           
$line fread ($file1028);
           
fwrite($fc,$line);
        }
        
fclose($fc);
        return 
true;
    }
}


$filename 'pathcopy2/file.ext';

if (
file_exists($filename)) {
$command_msg "The file exists...";
} else {
copyFile("pathcopyfrom/file.ext","pathcopy2/");



$to "me@me.com";
$from_header "From: me@me.com";
$subject "Cron Job Ran..";
$body "Cron details:";
$body .= "\n";
$body .= "....";
if (
mail($to$subject$body$from_header)) {
  
//echo("<p>Message successfully sent.</p>");
 
} else {
  
//echo("<p>Message delivery failed...</p>");
// }

?>


Thanks!

Offline MrPhil

  • Senior Moderator
  • Berserker Poster
  • *****
  • Posts: 6428
Re: Cron Issue Inquiry
« Reply #3 on: December 03, 2009, 05:49:21 PM »
Are pathcopyfrom and pathcopy2 absolute or relative paths? If relative, are you sure you know what directory you're starting in? In cPanel cron, it's normally /home/ACCOUNTNAME, but I don't know if it's different in LPCP cron. Generally it's safest to assume nothing about the path, and use absolute paths (starting with / ).

You should print (echo) the error message that the target file already exists. It will show up in the email sent by cron. Speaking of which, why are you bothering to send an email? The cron job should send you an email that includes anything written to stdout or stderr. It looks like this project was adapted from a browser-run script.

Try taking the '@' off the fopen() call. Maybe there's an error message that you'd like to see.

As a matter of style, I would suggest putting the two paths, and the filename, in three variables. Then you glue the appropriate path and the filename together when used. Is "pathcopyfrom" actually a URL rather than a directory path? There might be security restrictions going on (e.g., fopen barred from opening by URL) that stop you from opening a URL. If this is a directory-to-directory copy, using exec("cp ". $from . " " . $to); might be easier, but be sure to check if the target file already exists (it will be overwritten if you go ahead with the copy).

As for the path given in the Wiki, if they say /usr/bin/php5/bin/php for example, your cron command line would be /usr/bin/php5/bin/php -q /home/USER/public_html/PATH/copy_email.php. I don't see anything in your code that shouldn't work with PHP 4, so it's probably a moot point. Without the -q flag, you're probably seeing a line or two of "header" stuff (e.g., "text/html") in your email.
Visit My Site

E-mail Me
-= From the ashes shall rise a sooty tern =-

Offline applejax

  • Newbie
  • *
  • Posts: 5
Re: Cron Issue Inquiry
« Reply #4 on: December 03, 2009, 07:41:41 PM »
Oh dear. I didn't realize the path's have to be in PHYSICAL format in the php file too.. I shoulda figured..

Thanks much MrPhil, I'm sure that's the problem but I'll try the error report idea you had just to be sure.

meiere2

  • Guest
Re: Cron Issue Inquiry
« Reply #5 on: March 20, 2010, 08:06:05 AM »
Just a brief comment about cPanelX and LPCP.
In my opinion, they are actually quite different.  I have had an account on danzig for six years which uses cPanelX.  I now have a new account for a different web site on theta which uses LPCP.  It has been 3 months of hell.  Relevant to this subject, I have a script which I still can't get to run under Cron Jobs Manager on LPCP.  I runs from public_html and runs as a Cron Job on danzig but not  with LPCP.

Trying to get any information is like pulling teeth.  In particular, the 'old' function which would send an Email on execution of a Cron Job won't work under LPCP for me no matter what I do.

Offline MrPhil

  • Senior Moderator
  • Berserker Poster
  • *****
  • Posts: 6428
Re: Cron Issue Inquiry
« Reply #6 on: March 20, 2010, 10:45:54 AM »
I'm not on an LPCP server, so I can't tell you for sure what the differences are between cron on cPanel and cron on LPCP. Can you get an echo "Hello World" simple command line to run? That would prove that you've got the email address set up correctly. Then how about a simple "Hello World" script? Do you have any hard-coded paths to files or commands that you forgot to change (especially the account name), or to verify that they're in the same place on the new server? Don't forget that things like the path to Perl or PHP5 may be different. Scripts should never make any assumption about the default environment, especially $PATH, nor about what services are available or where files are installed. Explicitly give everything.

If you still can't figure it out, feel free to post a simplified version of the script here, and what your cron job command line looks like. It's probably something very simple that you're overlooking.
Visit My Site

E-mail Me
-= From the ashes shall rise a sooty tern =-

Offline lexhair

  • Galactic Royalty
  • *****
  • Posts: 427
Re: Cron Issue Inquiry
« Reply #7 on: March 22, 2010, 06:23:29 AM »
I have a cPanel account and a LPCP account. The main difference I see is that with the LPCP cron, you have to explicitly provide the path to php where in cPanel you don't (as long as you're using php version 4).

So my cron for my LPCP account would be:

Code: [Select]
/usr/bin/php -q /home/USERNAME/public_html/hello_world.php
I might be wrong about this but I will say that if you don't give the explicit path to php on your server, you risk not having the cron run.