LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Using cron to run PHP script (https://www.linuxquestions.org/questions/linux-general-1/using-cron-to-run-php-script-709424/)

GavB 03-05-2009 12:08 PM

Using cron to run PHP script
 
Hi, I hope someone can shed some light on this. I'm using a cron to run a PHP script every hour. The script runs with no problems at all, apart from one bit - I have uses the exec() function and that bit doesn't seem to work.

It works with no problems when running the script via SSH and the cron is set to run as root.

The code that is failing to run is:

exec('/full/path/to/file.sh');


Is this normal or have I done something wrong?

If it comes to it, then I can set the shell script to run under a separate cron job but it would be nice to have everything as a single one.

Thanks

JulianTosh 03-06-2009 06:34 AM

Might be a dumb question, but is the execute bit set on the file correctly? 775 perhaps? can you execute the script outside of the php script?

GavB 03-06-2009 11:20 AM

Thanks for your reply. The shell script that I'm trying to execute is set to 777 and does run if done manually through SSH. I can even run the PHP script and it all works, just not through cron.

colucix 03-06-2009 11:34 AM

You sure it does not execute at all? Maybe the script /full/path/to/file.sh gives some error and you don't get the expected result. Try to redirect the standard error to a file and see if you get some useful message.
Code:

0 * * * * script.php 2> $HOME/cron.log
maybe the unexpected behaviour is due to the limitation of the cron environment, as well.

GavB 03-06-2009 11:49 AM

I've found the problem I think - the shall script that runs also has a line of code to run another file and that just used a relative path. I've changed it to the full path so hopefully that will do the job.

Maybe I should have looked harder before posting!

GavB 03-06-2009 12:01 PM

Ignore the last message, didn't seem to do anything - I'll keep trying though!

colucix 03-06-2009 12:32 PM

Did you get some message from standard error? You can check the mail of the crontab's owner looking for messages from the cron daemon.

Also you can try the shell_exec() function or eventually back ticks in place of exec().

GavB 03-06-2009 12:36 PM

Hi, thanks for your replies - I have 100% found the problem now.

In the shell script, I was using the 'tc' command which works when running the script manually. It never occured to me to check for the full path of 'tc'. I've changed all instances of the command to '/sbin/tc' and it works perfectly.

I hope this helps someone in future, I imagine it's quite a common thing to do (unless I'm just and idiot) :D


All times are GMT -5. The time now is 09:12 PM.