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

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

问题描述

对不起,如果这是一个愚蠢的问题,但在现实中我没有线索。



我写了这个应用程序来压缩和将文件从客户端的PC移动到主电脑。

它可以作为一个网页。这是没有指导和没有规范的文档。

因此,作为命运,它应该是一个cron工作,将运行通过调用



***** runScript.php



所以我不知道在哪个顺序执行



我使用了两个AJAX调用来调用脚本中的方法,但是作为脚本运行文件有点不同。



任何人都可以指导我吗?



例如是有一个MAIN方法,将首先调用。








答案真的帮助全,但php文件将如何影响? @Ole Haugset 提到我应该修改它,因此使用 $ _ GET vars。这在脚本中已经通过 if($ _GET [argument] =='runScript'){} 来实现。



因此,将 runScript 附加到脚本调用应该可以正常工作?



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

解决方案

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



示例:

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

此脚本将每5分钟运行一周。无论PHP脚本回显/打印出来,都将存储到 LOG.txt 文件中,以便我可以监视脚本事件。



尝试在shell中运行命令,然后将其放在cronjobs中,以确保它可以正常工作。



但是,你说通常是通过AJAX调用来调用这个脚本。这不可能与cronjob做。我假设你使用AJAX传递一些$ _POST元素的脚本需要。所以你要做的是改编脚本以允许 $ argv 参数,并将它们添加到crontab作业,或者只是做一个不需要任何脚本



如果您要修改脚本以支持 $ argv 参数,请按照回答Stack Overflow上已经存在的关于添加参数的工作:



如何使用cron选项卡中的参数运行php url





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



  * * * * *要执行的命令
- - - - -
| | | | |
| | | | + -----星期几(0-6)(星期日= 0)
| | | + -------月(1 - 12)
| | + ---------月份(1 - 31)
| + -----------小时(0 - 23)
+ ------------- min(0 - 59)


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

Can anyone please guide me?

e.g. is there a MAIN method that will be called first. 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? @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.

So appending runScript to the script call should work fine?

e.g. * * * * * php /path/to/file/runScript.php runScript

解决方案

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.

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. Whatever the PHP-script echoes / prints out, will be stored to the LOG.txt file so that I can monitor the scripts events.

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

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.

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:

How to run a php url with parameters in cron tab

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.

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天全站免登陆