Page 1 of 1

Setting up a Cron

Posted: Mon Jun 16, 2008 8:56 pm
by aliasxneo
I'm a little familiar with Cron and setting it up on PHP but for some unknown reason I simply cannot get the job to run. Here is my PHP code:

Code: Select all

$command = "* * * * * /usr/local/bin/php /home/joshg/public_html/test.php"; 
$cron_file = "/home/joshg/public_html/cron/cronexec"; 
 
if (file_exists($cron_file)){
    file_put_contents($cron_file, $command);
    
    exec("crontab $cron_file");
} else {
    touch($cron_file);
    chmod($cron_file, 0777);
    
    file_put_contents($cron_file, $command); 
    
    exec("crontab $cron_file"); 
}
Pretty simple, if the cron file isn't there it creates it, otherwise it writes the given command and then calls crontab passing off the file path. Well according to the command given this job should call test.php every minute, and it's not. I've been debugging it for about 20 minutes now and have concluded the following:

1) I've called the "/usr/local/bin/php /home/joshg/public_html/test.php" command with PHP's exec() function and the script emailed me just like it should.
2) I went into CPanel and setup a nearly identical cron and it worked perfectly
3) I checked the cron file, the command looked fine

So basically I'm at a loss as to why it's not working. All I can assume is maybe it's something with the cron file being in my local directory? Any help would be appreciated. Thanks.

* EDIT *

I just called:

Code: Select all

echo exec("crontab -l");
And noticed that the cron is not updating. It updates fine if I do it on CPanel but for some reason my PHP script is not updating it. Any idea?

Re: Setting up a Cron

Posted: Tue Jun 17, 2008 6:35 am
by WebbieDave
This may not be a PHP code problem but a user permissions one. If your script is being invoked via mod_php, it's being run with the same user name as the web server. Therefore, the crontab command would be executed for that user.

Re: Setting up a Cron

Posted: Tue Jun 17, 2008 12:16 pm
by aliasxneo
WebbieDave wrote:This may not be a PHP code problem but a user permissions one. If your script is being invoked via mod_php, it's being run with the same user name as the web server. Therefore, the crontab command would be executed for that user.
I can run crontab -l though. I figured if I could run that I have permission to use crontab.

Re: Setting up a Cron

Posted: Tue Jun 17, 2008 12:29 pm
by WebbieDave
I'm assuming your php script fails only when invoked via the web server. If true, it's paramount to determine which user owns the process that is invoking your php script. If your server uses mod_php, it will be the user that also runs the web server. If php is invoked via cgi, it will most likely be the user account you've been assigned.

Also, you will need to ensure that safe mode isn't preventing you from running exec()

Re: Setting up a Cron

Posted: Tue Jun 17, 2008 12:51 pm
by aliasxneo
WebbieDave wrote:I'm assuming your php script fails only when invoked via the web server. If true, it's paramount to determine which user owns the process that is invoking your php script. If your server uses mod_php, it will be the user that also runs the web server. If php is invoked via cgi, it will most likely be the user account you've been assigned.

Also, you will need to ensure that safe mode isn't preventing you from running exec()
Well I don't have SSH Shell access so I can't try crontab via that but I would only assume it works if I can do it through CPanel. That's the thing that's weird to me. How come I can do it in CPanel but in in PHP?

And yes I know exec works because I've used the exact same PHP command in the cron command using exec and it worked perfectly.

Re: Setting up a Cron

Posted: Tue Jun 17, 2008 1:20 pm
by WebbieDave
aliasxneo wrote:How come I can do it in CPanel but in in PHP?
Perhaps because of process ownership. If CPanel is run under root, it can create cronjobs for anyone. Just find out which user is invoking your php (web) script.