LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-05-2009, 12:08 PM   #1
GavB
Member
 
Registered: Nov 2006
Posts: 54

Rep: Reputation: 15
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
 
Old 03-06-2009, 06:34 AM   #2
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
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?
 
Old 03-06-2009, 11:20 AM   #3
GavB
Member
 
Registered: Nov 2006
Posts: 54

Original Poster
Rep: Reputation: 15
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.
 
Old 03-06-2009, 11:34 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 03-06-2009, 11:49 AM   #5
GavB
Member
 
Registered: Nov 2006
Posts: 54

Original Poster
Rep: Reputation: 15
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!
 
Old 03-06-2009, 12:01 PM   #6
GavB
Member
 
Registered: Nov 2006
Posts: 54

Original Poster
Rep: Reputation: 15
Ignore the last message, didn't seem to do anything - I'll keep trying though!
 
Old 03-06-2009, 12:32 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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().
 
Old 03-06-2009, 12:36 PM   #8
GavB
Member
 
Registered: Nov 2006
Posts: 54

Original Poster
Rep: Reputation: 15
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)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script using /etc/cron.hourly to execute cron.php file? rioguia Programming 3 06-11-2008 08:09 AM
cannot get tu run a php script in cron alain Linux - General 6 02-06-2006 02:54 AM
php script will not run correctly as cron, fine from cli dtra Linux - Software 2 12-22-2005 07:21 AM
can't get cron to run my script. dr_zayus69 Linux - Software 6 04-12-2005 06:34 PM
Can I run localhost/file.php with CRON function kondor5 Linux - General 1 03-28-2004 06:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration