1

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.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.

Tom
  • 9,275
  • 25
  • 89
  • 147
  • for a start you need to add php, either just php or the full php path depending on config –  Mar 30 '11 at 20:40
  • add MAILTO="" at the top of the crontab file with your address, it will email you if there's a problem, great for testing –  Mar 30 '11 at 20:42

4 Answers4

5

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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
3

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

BMitch
  • 231,797
  • 42
  • 475
  • 450
0

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.

RDL
  • 7,865
  • 3
  • 29
  • 32
0

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.

Community
  • 1
  • 1
AndrewNimmo
  • 149
  • 1
  • 6