cronjob

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

cronjob

Post by sirTemplar »

hi. i have this php script that i want to run automatically every day at midnight. i edited crontab using vi command crontab -e. now when i crontab -l i see what i did:

0 0 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
,21,22,23,24,,25,26,27,28,29,30,31 * * /fullpath/to/my/phpscript/script.php

now, how do i know that this works? is this enough or do i have to include something on my script? another thing, the file script.php is under a protected directory. thanks.
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Post by leenoble_uk »

You could have saved youself some typing there:
0 0 * * * /path/to/file
would do it every day at midnight.
Whether this works or not all rather depends on if you have the cgi version of php installed.
The regular PHP install is an Apache module and won't run outside of a request from a web browser so you could theoretically write a script to launch your broswer and request this php script but that would be silly.
If you've got the cgi version then I think it should work. I'm not certain as I only have the module version.
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

Post by sirTemplar »

:) thx for the reply. i have a cgi version installed. so i will just have to wait tomorrow morning and check if the script work..
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

Post by sirTemplar »

the cron job is not working at all!
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

If your PHP isn't able to run from the command-line, use a console-based browser (such as lynx) to browse the script page (leenoble_uk - it's actually more common than you think :))

Have you tried using another command in your cron job to test that it's actually doing something? I suggest "touch /some/file/here", then you can see from its timestamp the last time that job ran.
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Post by leenoble_uk »

As I said, I don't have the cgi version of PHP so this may not be correct but you might need to make the script an executable.

chmod +x /path/to/cgi-php/script/nameoffile.php

Is there any reason that you can't increase the frequency of the cron in order to see if it's working?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Have a look at these this tutorial and this.
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

Post by sirTemplar »

i think i figured out something. the path to php is php4. now i have this:

5 * * * * /usr/local/lib/php4 /path/to/script/auto/script.php

this should execute script.php every 5 minutes. when i type

/usr/local/lib/php4 /path/to/script/auto/script.php

on the command line, the script works. but the cronjob doesn't work!!!

any idea?
leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

Post by leenoble_uk »

Did you add the crash-bang or whatever it's called line in the tutorial above?

And that doesn't execute every 5 minutes. That executes at 5 past the hour

/5 * * * * /command

does it every 5 minutes.
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

Change the PHP call to "touch /tmp/touched", and then see if the file /tmp/touched has its timestamp updated. That's a good way to tell if cron is working properly, as it's rare to get problems touching a file you made, especially in the temp directory. If it's working, you know it's something to do with your PHP file, most likely permissions.

dave
sirTemplar
Forum Commoner
Posts: 65
Joined: Wed Dec 18, 2002 1:57 am

Post by sirTemplar »

the cron works perfectly now. thanks for all the help!!! :o
Pozor
Forum Commoner
Posts: 74
Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland

Post by Pozor »

Hello,

what was the final solution, what was the mistake?

greez Pozor
Post Reply