如何编写 PHP cron 脚本? [英] How can I write a PHP cron script?

查看:29
本文介绍了如何编写 PHP cron 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个愚蠢的问题,我很抱歉,但实际上我不知道.

I'm sorry if this is a silly question, but in reality I have no clue.

我编写了这个应用程序来压缩文件并将文件从客户端的 PC 移动到主 PC.
它作为网页工作得很好.这是在几乎没有指导和规范文档的情况下完成的.
因此,正如命运所愿,它应该是一个通过调用

I wrote this app to compress and move files from a client's PC to a master 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.

我使用了 2 个 AJAX 调用来调用脚本中的方法,但仅将文件作为脚本运行就有点不同了.

I used 2 AJAX calls to call methods in the script, but just running the file as a script is n bit different.

有人可以指导我吗?

例如是否有首先调用的 MAIN 方法.如果是这样,那么我可以从那里指导脚本.

e.g. is there a MAIN method that will be called first. and if so, then I can guide the script from there.

更新

答案真的很有帮助,但是php文件会受到什么影响呢?@Ole Haugset 提到我应该调整它,所以使用 $_GET 变量.例如,这已在脚本中通过 if ($_GET["argument"]=='runScript'){} 完成.

The answers are really help full, but how would the php file be affected? @Ole Haugset mentioned that I should adapt it so use $_GET vars. This is done in the script already by if ($_GET["argument"]=='runScript'){} for instance.

那么将 runScript 附加到脚本调用应该可以正常工作吗?

So appending runScript to the script call should work fine?

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

推荐答案

将 PHP 脚本作为 cron 作业运行没有问题.您需要做的是在文件系统中提供脚本文件的完整路径,并使用命令 php 告诉 Linux 运行该文件的程序.

There are no problems running a PHP script as a cron job. 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.

示例:

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

此脚本将每 5 分钟运行一次,每周的所有天都如此.无论 PHP 脚本回显/打印出什么,都将存储到 LOG.txt 文件中,以便我可以监控脚本事件.

This script will run every 5 minutes, all days of the week. Whatever the PHP-script echoes / prints out, will be stored to the LOG.txt file so that I can monitor the scripts events.

在将命令放入 cronjobs 之前,尝试只在 shell 中运行该命令以确保它有效.

Try running just the command in your shell before putting it in the cronjobs to make sure it works.

但是,您说您通常使用 AJAX 调用来调用此脚本.使用 cronjob 将无法做到这一点.我假设您使用 AJAX 传递脚本需要的一些 $_POST 元素.因此,您需要做的就是调整脚本以允许 $argv 参数,并将它们添加到 crontab 作业中,或者简单地制作一个在运行之前不需要任何给定参数的脚本.

However, you say that you normally call this script with a AJAX call. This will not be possible to do with a cronjob. I assume you use AJAX to pass along some $_POST-elements that the script needs. 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 参数,请遵循 Stack Overflow 上已有的关于向作业添加参数的答案:

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:

如何使用cron选项卡中的参数

我只想从下面的答案中添加到我的答案中.要在 Linux 中编辑您的 crontab 作业,您只需使用命令 crontab -e.

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.

这是需要填写的每个必填参数的描述.

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)

这篇关于如何编写 PHP cron 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆