开发者

How to make a php cronjob under Debian

I'm trying to set a cronjob to run every 20 minutes. The file path is /srv/www/mysite.co.uk/public_html/PP/Make_Xml.php

but i need to transfer to it a var so basically to cron: /srv/www/mysite.co.uk/public_html/PP/Make_Xml.php?db=LONDON

I tried to use "crontab -e" and set it even to every minute with:

 * * * * * /srv/www/mysite.co.uk/public_html/PP/Make_Xml.开发者_JS百科php?db=LONDON

it saved it to /tmp/crontab.something/crontab

And it doesn't seem to work. I'm new to linux please help.


First of all, when calling a PHP script from the command line, you will not pass it parameters the way you did here.

You'll typically pass those like this :

/srv/www/mysite.co.uk/public_html/PP/Make_Xml.php db=LONDON

And, from your PHP script, you will not get the data into $_GET, but into $_SERVER['argv']


For example, if I create a temp.php script that contains this :

<?php
var_dump($_SERVER['argv']);

Calling it this way :

php temp.php db=LONDON

will get me the following output :

array(2) {
  [0]=>
  string(8) "temp.php"
  [1]=>
  string(9) "db=LONDON"
}


Then, note you should probably call the php executable program, from your crontab, and not directly the PHP script -- unless you made it executable.

Which probably means using something like this :

* * * * * /usr/bin/php /srv/www/mysite.co.uk/public_html/PP/Make_Xml.php db=LONDON

Note : you may need to adapt the path to php.


Call it with php:

* * * * * /usr/bin/php /srv/www/mysite.co.uk/public_html/PP/Make_Xml.php?db=LONDON

For the db=LONDON part, you may need to just pass LONDON as a command line arg. See the following site for more details on that: http://www.php.net/manual/en/features.commandline.usage.php


See Pascals response re: variables.

For every 20 minutes you want:

*/20 * * * * /usr/bin/php /srv/www/mysite.co.uk/public_html/PP/Make_Xml.php db=LONDON

Which says to run the script whenever the minutes is divisible by 20.


There are 2 additional options you could consider.

For a PHP script which you intend using from the command-line, you can use the PEAR Console_Getopt command-line option parser which offers similar functionality to getopt libraries for other languages.

If your PHP script needs to run in a web context, you can use a cron job to make requests for the URL. A stackoverflow question and answer covers this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜