Possible Duplicate:
Cron job on Ubuntu for php
I am running and ubuntu server and wanted to run a php script every day. I have done some research and found that cron is the best way of doing this however, this is where i got stuck, a lot of the information on the internet about cron is very hard to follow and understand.
So i wanted to execute a simple php script once a day, the script i made for testing simply just deletes a record from a database, but the real script will do a lot more.
I tried setting up a task through plesk which is provided through my web host service but it didn’t seem to execute when i wanted it to, i used 1 for minutes, 22 for hours, * for day, * for week and * for month and thought this would execute every day at 22:01.
I have the directories on my server:
cron.hourly
cron.daily
cron.weekly
cron.monthly
I thought i could dump i file in there and it would execute for example every day, but i’m guessing i need to make a cron script to call a php script right?
If i were to go the way of putting a file in the cron.daily folder how would i go about it?
Also if there are any steps i need to take on the php side please let me know?
Thanks a lot for your time.
2
Answers
There’s couple of ways to setup cron job. Assuming you got shell access you could do
crontab -e
from console and define job there, i.e. like this:which would trigger
command
(whatever it is) at 22:01 each day (not sure why you set minutes to1
instead of0
though). To launch PHP script from there you would either have to installphp-cli
, and then invoke it that way:You can also call bash script here, to setup all the stuff like paths etc and then call your php script form bash – sometimes it is simpler to do that way instead of crafting way too long command line for cron. And it’s simpler to update it later. also, you could turn your php script into bash-runnable script by setting it execution bit (
chmod a+x script.php
) and adding shell’s shebang:If your script got too many dependencies and you’d prefer to call it via web, you could use
wget
to mimic a browser. so yourcommand
would be:wget manual can be accessed by
man wget
orwget -h
, or is on this website. Aternatively you may useHEAD
tool fromperl-www
package – but it requires perl while wget is a standalone tool. If you use HTTPS with self signed certs, add--no-check-certificate
to your invocation arguments. And you may also want to setup.htaccess
and limit web access to your cron script to localhost/127.0.0.1every minute:
every 24hours (every midnight):
Se this reference for how crontab works: http://adminschoice.com/crontab-quick-reference, and this handy tool to build cron jobx: http://www.htmlbasix.com/crontab.shtml