简体   繁体   English

如何编写PHP cron脚本?

[英]How can I write a PHP cron script?

I'm sorry if this is a silly question, but in reality I have no clue. 很抱歉,这是一个愚蠢的问题,但实际上我没有任何线索。

I wrote this app to compress and move files from a client's PC to a master PC. 我编写了这个应用程序,用于将文件从客户端PC压缩和移动到主PC。
It works well as a web page. 它可以很好地用作网页。 This was don with little guidance and no spec doc. 这是唐,几乎没有指导,也没有规范文档。
So as fate would have it, it was supposed to be a cron job which would run by calling 因此,按照命运的安排,这应该是一项计划工作,可以通过致电

*****runScript.php

So I don't know in which order the execution will or should take place. 因此,我不知道执行将以或应以哪个顺序进行。

I used 2 AJAX calls to call methods in the script, but just running the file as a script is n bit different. 我使用了2个AJAX调用来调用脚本中的方法,但是仅将文件作为脚本运行并没有什么不同。

Can anyone please guide me? 谁能指导我?

eg is there a MAIN method that will be called first. 例如,是否有将首先调用的MAIN方法。 and if so, then I can guide the script from there. 如果是这样,那么我可以从那里指导脚本。

UPDATED 更新

The answers are really help full, but how would the php file be affected? 答案确实可以帮助您解决所有问题,但是php文件将如何受到影响? @Ole Haugset mentioned that I should adapt it so use $_GET vars. @Ole Haugset提到我应该对其进行调整,因此请使用$_GET vars。 This is done in the script already by if ($_GET["argument"]=='runScript'){} for instance. 例如,在脚本中已经通过if ($_GET["argument"]=='runScript'){}了此操作。

So appending runScript to the script call should work fine? 因此,将runScript附加到脚本调用应该可以正常工作吗?

eg * * * * * php /path/to/file/runScript.php runScript 例如* * * * * php /path/to/file/runScript.php runScript

There are no problems running a PHP script as a cron job. 将PHP脚本作为cron作业运行没有问题。 What you need to do is provide the full path in the filesystem to the script file, and use the command php to tell Linux what program to run the file with. 您需要做的是在文件系统中提供脚本文件的完整路径,然后使用php命令告诉Linux使用什么程序运行文件。

Example: 例:

*/5 * * * * php /var/www/vhosts/statistikk/cron/getLastArticleFromIntranet.cron.php >> /var/www/vhosts/statistikk/cron/LOG.txt 2> /dev/null

This script will run every 5 minutes, all days of the week. 该脚本将在一周的所有天中每5分钟运行一次。 Whatever the PHP-script echoes / prints out, will be stored to the LOG.txt file so that I can monitor the scripts events. 不管PHP脚本回显/打印出什么内容,都将存储到LOG.txt文件中,以便我可以监视脚本事件。

Try running just the command in your shell before putting it in the cronjobs to make sure it works. 在将命令放入cronjobs中之前,请尝试仅在外壳中运行该命令以确保其正常工作。

However, you say that you normally call this script with a AJAX call. 但是,您说通常使用AJAX调用来调用此脚本。 This will not be possible to do with a cronjob. 这将与cronjob无关。 I assume you use AJAX to pass along some $_POST-elements that the script needs. 我假设您使用AJAX传递脚本需要的一些$ _POST元素。 So what you have to do is either adapt the script to allow $argv parameters as well, and add them to the crontab job, or simply make a script which does not need any given parameters before it runs. 因此,您需要做的就是要么使脚本也允许$argv参数,然后将它们添加到crontab作业中,要么干脆编写一个不需要任何给定参数的脚本即可运行。

If you are going to adapt your script to support $argv parameters, follow the answer already existing on Stack Overflow about adding parameters to the job: 如果要修改脚本以支持$argv参数,请遵循Stack Overflow中已经存在的关于向作业添加参数的答案:

How to run a php url with parameters in cron tab 如何在cron选项卡中使用参数运行php url

EDIT: 编辑:

I'd just like to add to my answer as from the answer below. 我只想从下面的答案中添加到我的答案中。 To edit you crontab jobs in Linux you can simply use the command crontab -e . 要在Linux中编辑crontab作业,您只需使用命令crontab -e

This is the description of each required param that needs to be filled. 这是需要填写的每个必需参数的描述。

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of        month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

To add a cronjob in linux do this: 要在Linux中添加cronjob,请执行以下操作:

# crontab -e if vi is your editor press i to enter text and esc to exit. #crontab -e如果vi是您的编辑器,请按i输入文本,按esc退出。 Use :w to save your changes and :q to quit. 使用:w保存更改,并使用:q退出。 If nano is your choosen editor, then use strg+o for saving and str+x for closing. 如果nano是您选择的编辑器,则使用strg + o进行保存,使用str + x进行关闭。

Anyway add this line to the bodom of the file: */5 * * * * php runScript.php 无论如何,将以下行添加到文件的正文中:* / 5 * * * * php runScript.php

It will be called every 5min. 每隔5分钟就会调用一次。

* * * * * php /path/to/file/runScript.php
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └──── Day of the week (0-7, Sunday is 0 or 7)
│ │ │ └────── Month (1-12)
│ │ └──────── Day (1-31)
│ └────────── Hour (0-23)
└──────────── Minute (0-59) 

That should be it :) 应该就是这样:)

A common method for running PHP scripts from a cron job is to use a command-line program such as curl or wget. 从cron作业运行PHP脚本的常用方法是使用命令行程序,例如curl或wget。 For example, the cron job runs a command similar to the following command: 例如,cron作业运行类似于以下命令的命令:

curl http://*****.com/script.php

n this command, curl retrieves the web page, which then runs the PHP script. 在此命令中,curl检索网页,然后该网页运行PHP脚本。

However, there is a better way to run PHP scripts on your web site from cron jobs. 但是,有一种更好的方法可以通过cron作业在您的网站上运行PHP脚本。 You can run the script directly by using the PHP command-line interpreter. 您可以使用PHP命令行解释器直接运行脚本。 This method is just as effective, and usually faster. 这种方法同样有效,通常更快。 The following command shows how to run a script using the PHP command-line interpreter: 以下命令显示了如何使用PHP命令行解释器运行脚本:

/usr/local/bin/php -q /home/username/public_html/script.php

In this example, the PHP command-line interpreter runs the script.php file. 在此示例中,PHP命令行解释器运行script.php文件。 The -q option enables quiet mode, which prevents HTTP headers from being displayed. -q选项启用安静模式,该模式可防止显示HTTP标头。

Depending on the code in your PHP script, it may only run correctly when called from a specific directory. 根据PHP脚本中的代码,仅当从特定目录中调用时,它才能正确运行。 For example, if the script uses relative paths to include files, it will only run if it is called from the correct directory. 例如,如果脚本使用相对路径包括文件,则只有从正确的目录中调用脚本时,脚本才会运行。 The following command shows how to call a PHP script from a specific directory: 以下命令显示如何从特定目录调用PHP脚本:

cd /home/username/public_html/; /usr/local/bin/php -q script.php

If your script requires special configuration options, you can use a custom php.ini file. 如果您的脚本需要特殊的配置选项,则可以使用自定义php.ini文件。 The -c option allows you to call a PHP script using a custom php.ini file: -c选项允许您使用自定义php.ini文件调用PHP script

/usr/local/bin/php -c /home/username/php.ini /home/username/public_html/script.php

Click Here to More About Cron PHP Script Define with Example. 单击此处以了解有关Cron PHP Script示例的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM