Web Hosting Talk







View Full Version : crontab to run webpage


Richard Tan
07-22-2003, 01:07 AM
Hi,

I am new to crontab. Can anyone pls let me know how to setup a script to run a .php file at pre-determined time, using crontab?

Thanks a lot!!

LiveRack
07-22-2003, 01:15 AM
I'm guessing you just need to know the command to input in the cpanel crontab setup box. There is more than one way to do it but the command I put in crontab is:

wget -O - http://www.mydomain.com/run.php -q

Richard Tan
07-22-2003, 01:23 AM
Thanks nacs! I'm using hsphere so the in crontab I just need to input the minutes, days etc. at respective fields.

BTW, do I just input "http://www.mydomain.com/run.php -q" in the command field, & crontab will run the file at pre-set time? I've tried this but won't work... is "-q" means "quit"?

Thanks again!

Jasber
07-22-2003, 01:25 AM
If the php file is on your box you can do 'crontab -e' and then do


* * * * * * php /path/to/php/script.php


Obviously you'll have to edit the times, for the script to run, I got this little chart here http://www.nerc-online.com/support/www/crontab.html

minute hour day month weekday command
Minute - Minutes after the hour (0-59).
Hour - 24-hour format (0-23).
Day - Day of the month (1-31).
Month - Month of the year (1-12).
Weekday - Day of the week. (0-6; the 0 refers to Sunday).


Hope this helps.

LiveRack
07-22-2003, 01:34 AM
No Richard, you have to type in "wget -O - http://www.mydomain.com/run.php -q" (with the wget). The -q stands for 'quiet mode' which suppresses errors and output (but still runs the script).

Richard Tan
07-22-2003, 02:03 AM
Thanks Kfreak I learnt a lot from your article.
nacs thanks again. I'll try out your method later, but can you enlighten me what's the meaning of "wget - O -"?

Many thanks for helps.

LiveRack
07-22-2003, 02:14 AM
wget is a commandline webpage/file downloader. By executing it through cron with a URL, you'll be causing your server to execute the file as though you'd surfed over to that page yourself. the -O flag means output to the screen instead of saving the file.

Richard Tan
07-22-2003, 02:23 AM
Thanks nacs I will try to run your script.

Richard Tan
07-22-2003, 06:43 AM
Hi nacs, can I just leave out the -O because I just want the server to execute the file without any output to screen. Both the file and the server are remote, so there's no way I can view the file's result.

Thanks!

LiveRack
07-22-2003, 10:35 AM
Yes you could leave out the -O if you wish.

Richard Tan
07-22-2003, 06:33 PM
the correct syntax is "/usr/local/bin/wget http://mydomain.com/script.php -q".

LiveRack
07-22-2003, 06:43 PM
In cpanel, the stuff I pasted to you is exactly how I have it except for a different URL (and it works). :)

Richard Tan
07-22-2003, 08:26 PM
maybe hsphere requires full path to wget. anyway it works fine. Thanks.

Richard Tan
07-22-2003, 08:41 PM
nacs a quite one: what's the option if I don't want to save the file?

LiveRack
07-22-2003, 08:54 PM
I haven't found the exact switch but you can use the -O flag (output to screen instead of saving) to have it not save. If that results in output getting emailed to you everytime it runs (it doesn't email anything in cpanel) then you can use this:

wget -O - http://mydomain.com/script.php -q > /dev/null

So basically:

-O for output to screen
-q for quiet mode
> /dev/null to redirect output to /dev/null (essentially a black hole for output).

Richard Tan
07-22-2003, 10:32 PM
I've tried -o yesterday, but the file was still downloaded & saved on my server. If -q is added, I won't receive a confirmation email.

Will try /dev/null later. Thanks.

LiveRack
07-22-2003, 11:24 PM
Switches are case sensitive so you may want to try a capital 'oh' instead of a lowercase.

Richard Tan
07-23-2003, 01:20 AM
Extract from wget documentation:

`-O file'
`--output-document=file'
The documents will not be written to the appropriate files, but all will be concatenated together and written to file. If file already exists, it will be overwritten. If the file is `-', the documents will be written to standard output. Including this option automatically sets the number of tries to 1.

So you could be right, I will try it out now.

Richard Tan
07-23-2003, 02:10 AM
nacs,
if I execute "wget -O - http://mydomain.com/script.php -q" it will save script.php as "-q"

I'ven't tried " /dev/null" but you mentioned earlier that it's a black hole of output. why is it so? does it mean that the file will be saved there? I so I'm afraid they all will get piled up.

Thanks!

LiveRack
07-23-2003, 02:18 AM
if I execute "wget -O - http://mydomain.com/script.php -q" it will save script.php as "-q"

Are you stating this as fact because that's what happened or are you guessing? Because for me it does not save (and it shouldn't since the -O - redirects to stdout instead of to file).

/dev/null is a virtual device in linux (and some other OSs) that basically is like a black hole. Anything that is directed/piped/outputted there will not be saved or shown or displayed. Think of it as a bottomless pit for your files/output.

If you have problems, you could leave the -q flag out and just redirect to /dev/null and it should result in no output.

Richard Tan
07-23-2003, 02:44 AM
I actually tried that script out, and I've got myself a file named -q :{ at my document root.

I will follow your advice & leave out the -q switch & use /dev/null.