Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Crontab - executing a PHP script 1

Status
Not open for further replies.

monkey64

Technical User
Apr 27, 2008
69
0
0
GB
My host has PHP compiled as an Apache module.

I am hoping to run the cron.php file every 5 minutes. The file is just a test and sends me an email. That's all.
Either I have specified the wrong path or my syntax is wrong because it is not working.

Code:
MAILTO=myemailaddress.com
*/5 * * * * /home/xxx/webs/xxx/htdocs/cron.php
then tried:

Code:
MAILTO=myemailaddress.com
*/5 * * * * php -q /home/xxx/webs/xxx/htdocs/cron.php

What am I doing wrong?
 
the important question is does the script mail you when you run the command in the cli? Also do you have the MAILTO line in the cron or somewhere else? Have you checked /var/log/messages for any clues as to why it's not running?
 
Hi

We need to know what that "is not working" means.

As w1nn3r mentioned, the logs could provide an explanation, however the relevant log file may or may not be /var/log/messages. ( On my system it is /var/log/cron . ) But some hosts not allow access to log files.

Another question is, what kind of access you have to the host ? Shell access would be the most helpful, so you can use the [tt]which[/tt] command or the [tt]type[/tt] shell builtin ( in case of sh-compatible shells, like Bash and Ksh ). If none is available, the [tt]whereis[/tt] command could also shed some light on where is the [tt]php[/tt] executable stored. But some hosts not install the PHP CLI.

Anyway, most probably the problem is finding the interpreter. The environment provided by [tt]cron[/tt] to the executed commands is slightly different than the interactive shell's environment, because some rc scripts are not executed. So PATH may not contain some directories. That is why you have to specify the full path to the executables :
Code:
MAILTO=myemailaddress.com
*/5 * * * * [highlight]/usr/bin/[/highlight]php -q /home/xxx/webs/xxx/htdocs/cron.php


Feherke.
 
When I say "is not working", I mean that my php script is not being executed. If it was, I would be receiving an email.
I am on a shared server with limited shell access. I don't think I have access to the log files. I can run execute my php script from within the shell successfully, and I receive a test email. All I want to do is automate this!

My hosting company have a support page for crontabs:
Are there any clues to find out the interpreter?

Thanks
 
Can you do a which php and replace the php in your crontab entry with the full-path version as suggested by Feherke?

The internet - allowing those who don't know what they're talking about to have their say.
 
Okay. I have entered <b>which php</b> in the shell.
It gave me <b>/usr/bin/php</b>

Code:
MAILTO=myemailaddress.com
*/5 * * * * /usr/bin/php -q /home/xxx/webs/xxx/htdocs/cron.php

That is what I have got already?
BTW. How long after saving the crontab would you expect to wait before the change is updated?

Tearing my hair out.
 
Just for S&G as a temporary testing thing, set the permissions of that htdocs directory as 777 and see if maybe there is a permissions thing going on.

What permissions is the cron stuff that YOU have access to running under? Is it the same permissions as the system cron is running under? I am thinking that is not the case because otherwise a non-privilged user might be able to run something bad if they put it in a script....

Just ramblings of things that have bit me in previous lives....
 
Just had a thought...
Should I expect an error email upon unsuccessful execution? I presume that's the point of the MAILTO tag. I'm getting nothing.

As for permissions, have set my htdocs dir to 777. Still no luck.
 
Got it work . Eventually...
Seems I didn't need to specify the full path after all.

Code:
MAILTO=myemailaddress.com
*/5 * * * * /usr/bin/php webs/xxx/htdocs/cron.php

Thanks to all.

 
Hi

Just to have all questions answered, maybe a future reader will need them too.
monkey64 said:
How long after saving the crontab would you expect to wait before the change is updated?
If you edit it using the [tt]crontab -e[/tt] command, changes should be applied immediately. If not, depends on the [tt]cron[/tt] implementation :
[ul]
[li]Old [tt]cron[/tt]s used to wake up each minute to check if there is something to do. For those usually not matters how the crontab was edited.[/li]
[li]Modern [tt]cron[/tt]s used to wake up only when they have a task to run. Such [tt]cron[/tt]s will not notice if the crontab was edited, so the [tt]crontab -e[/tt] command sends a notification to the daemon. If you edit the crontab in other ways, you have to send the notification yourself.[/li]
[/ul]
monkey64 said:
Should I expect an error email upon unsuccessful execution?
The usual behavior of [tt]cron[/tt] is to send the tasks' output in mail. If a task produces no output, no mail is sent.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top