How to execute a php file with crontab?

2

1

I have ubuntu setup with lamp and I was wondering what I would type to start a php page say at 2am everyday. I am still studying how crontab works so I can't test to see which one works.

So if I have something like this how would I put in the php page with parameters?

30 18 * * * http://myip/mypage.php?action=do_something

Would I enter it like that or with absolute path? Like this

30 18 * * * /var/www/mypage.php?action=do_something

Sarmen B.

Posted 2011-04-11T04:40:27.493

Reputation: 599

Answers

1

You definitely want to use method one. That is

30 18 * * * wget -O - -q -t http://myip/mypage.php?action=do_something

As this will make the file be fetched with wget (a file downloading program) through the usual web route. This means that when apache handles the request, it will cause php to interpret that script and you would have run the script.

I'm fairly certain it's also possible to run this using method 2, by calling the php-cli application and passing the path to your script as an argument, but in most cases that would not make any real difference.

Varun Madiath

Posted 2011-04-11T04:40:27.493

Reputation: 383

0

The command you place in your cron depends on how the php file is actually written. If the file is a script under a webserver then wget as mentioned will work fine. Notice with the command above the -O means the output will be written to file, -q is quiet, -t is for limiting the number of retries.

However if the php script is a shell script, wget is useless. In that case the file has to be executable and your cron command would look like:

30 18 * * * /usr/bin/env php /path/to/script

Ketema

Posted 2011-04-11T04:40:27.493

Reputation: 683