Page 1 of 1

CRONTAB with PHP - HUGE PROBLEM!

Posted: Mon Feb 22, 2010 6:49 am
by luyznascimento
Hi,

I tried everything since yesterday up to now, but, unfortunatly, I could not solve this problem. The one I describe below:


Let's go:

1 - I have a DIR on server where its generated some files. So I did a script (PHP) to read those files and do some actions. No big deal until now. This action works pefectly when executed as browser's link. This was the first step to avoid any stupid mistakes when cron is aplied.

2 - I read a bunch of informations related to CRON. I have ROOT access and I used: "crontab -e" command to make things "up".

3 - I will inform below in order of atitudes done (so hard for something apparently simple) so all of you can keep track of my steps:

3.1 - Runing crontab -e:

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
*/1 * * * * root php "path to my php file" >> "path to the log file" 2>&1

(1) The user is root. Note either that cron will run every minute to fit my needs.

(2) I was using, before this, a third file (sh) : instead of "path to my php file" I was using: "path to sh file [and this file calls the php file]". Of course, in time, that all these files have CHMOD "+x" applied.
(3) The php file has the following config in the first line:

Code: Select all

#!/usr/bin/php -q
and just below I got the rest of code: <?php // my code; ?>

4 But, when the cron it s requested nothing happen to the file but generates the following log error: /bin/sh: root: command not found.

RESEARCHES
===========

I did a lot of researchs around. One of the most importants stated that to make use of : #!/usr/bin/php -q - php must run in CGI mode on LINUX. That s not my case. My dist run as APACHE HANDLER. I try to call LYNX or WGET there on CRONTAB but with no success. But, I call the file (php file) on terminal like: lynx http://www.mysite.com/myfile.php - everything works properly! Or if I use ./myphpfile.php - it s executed the same with success. But with CRON - no deal!

That's all! I hope, sincerelly, someone could help me!

Luyz

Re: CRONTAB with PHP - HUGE PROBLEM!

Posted: Mon Feb 22, 2010 9:09 am
by AbraCadaver
root is not a valid command obviously. Also, I'm not sure if the environment variables can be set in the crontab. They should already be set if the crontab is for root. But to be complete, use the path in the command line:

Code: Select all

*/1 * * * * /path/to/php "/path/to/my/php/file.php" >> "/path/to/the/log/file" 2>&1

Re: CRONTAB with PHP - HUGE PROBLEM!

Posted: Wed Feb 24, 2010 12:06 pm
by luyznascimento
AbraCadaver wrote:root is not a valid command obviously. Also, I'm not sure if the environment variables can be set in the crontab. They should already be set if the crontab is for root. But to be complete, use the path in the command line:

Code: Select all

*/1 * * * * /path/to/php "/path/to/my/php/file.php" >> "/path/to/the/log/file" 2>&1

Hi,


Thank you for your attention. I will take a look the way you explained above and come back to let all know if it works.

Regards

Re: CRONTAB with PHP - HUGE PROBLEM!

Posted: Wed Feb 24, 2010 1:17 pm
by luyznascimento
luyznascimento wrote:
AbraCadaver wrote:root is not a valid command obviously. Also, I'm not sure if the environment variables can be set in the crontab. They should already be set if the crontab is for root. But to be complete, use the path in the command line:

Code: Select all

*/1 * * * * /path/to/php "/path/to/my/php/file.php" >> "/path/to/the/log/file" 2>&1

Hi,


Thank you for your attention. I will take a look the way you explained above and come back to let all know if it works.

Regards

AbraCadaver,


You´re completly right! I did not know that "root" on crontab -e - it means a command. I thought it was the USER (owner) to execute that action!

Now - on "crontab -e" I have:
*/30 * * * * /bin/bash /path/to/sh/file >> /path/to/log/file
SH FILE - paths to php file ... this was the way i was doing...and now worked properly!

Thank You!