Linux 如何在Debian下制作php cronjob

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5492068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:28:48  来源:igfitidea点击:

How to make a php cronjob under Debian

phplinuxcrondebian

提问by Tom

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

我正在尝试将 cronjob 设置为每 20 分钟运行一次。文件路径为/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

但我需要向它转移一个 var 所以基本上到 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:

我尝试使用“crontab -e”并将其设置为每分钟:

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

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

它保存到 /tmp/crontab.something/crontab

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

它似乎不起作用。我是 linux 新手,请帮忙。

采纳答案by Pascal MARTIN

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

首先,当从命令行调用 PHP 脚本时,您不会像这里那样传递参数

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']

而且,从您的 PHP 脚本中,您不会将数据放入$_GET,而是放入$_SERVER['argv']


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


例如,如果我创建一个temp.php包含以下内容的脚本:

<?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 phpexecutable program, from your crontab, and not directly the PHP script -- unless you made it executable.


然后,请注意,您可能应该从 crontab调用php可执行程序,而不是直接调用PHP 脚本——除非您使其可执行。

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.

注意:您可能需要将路径调整为php.

回答by BMitch

Call it with php:

用 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

对于 db=LONDON 部分,您可能只需要将 LONDON 作为命令行参数传递。有关更多详细信息,请参阅以下站点:http: //www.php.net/manual/en/features.commandline.usage.php

回答by RDL

See Pascals response re: variables.

请参阅 Pascals 响应:变量。

For every 20 minutes you want:

您想要的每 20 分钟:

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

这表示只要分钟可被 20 整除就运行脚本。

回答by AndrewNimmo

There are 2 additional options you could consider.

您可以考虑另外 2 个选项。

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

对于您打算从命令行使用的 PHP 脚本,您可以使用 PEAR Console_Getopt命令行选项解析器,它提供与其他语言的 getopt 库类似的功能。

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

如果您的 PHP 脚本需要在 Web 上下文中运行,您可以使用 cron 作业来请求 URL。一个计算器问题和答案涵盖这一点。