简体   繁体   English

设置Cron作业以执行PHP

[英]Set Up Cron Job To Execute PHP

I have a php page with a text box that accepts a player's id number, makes a request to another page based off that id, using a GET request, and then uses the response. 我有一个带有文本框的php页面,该文本框接受玩家的ID号,使用GET请求基于该ID向另一个页面发出请求,然后使用响应。

This part works perfectly. 这部分工作完美。 However now I would like to automate the process using a Cron job. 但是,现在我想使用Cron作业自动化该过程。

I have never used a Cron job before so I am a bit confused on their execution. 我以前从未使用过Cron作业,因此我对执行它们有些困惑。

Following this documentation though: [http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/][1 不过请遵循以下文档:[http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/][1

I am attempting to set one up. 我正在尝试设置一个。 Firstly, after opening the crontab through crontab -e if I wanted to execute my php page every minute I'd write: 首先,通过crontab -e打开crontab后,如果我想每分钟执行一次php页面,我会写:

1 0 0 0 1 /updateplayer.php

My first question is, do I have to write a job for each day of the week if i want it to run every day every minute or is there an all days option not listed in this documentation? 我的第一个问题是,如果我想每天每分钟运行一次,是否必须在一周的每一天写一份工作,或者本文档中没有列出全天候选项?

Secondly, in order to get the "important" part of that php page to run, I need to have my $_GET['playerid'] set to a certain player id. 其次,为了使该php页面的“重要”部分运行,我需要将$_GET['playerid']设置为某个玩家ID。 I'm hoping I might accomplish this through part of the arg1 arg2 portions, (following the phpexecute.php part of my proposed cron job). 我希望我可以通过arg1 arg2部分的一部分来做到这一点(在我建议的cron作业的phpexecute.php部分之后)。 However this is not working as well as expected. 但是,这种方法的效果不如预期。 Can anyone help me to get this job running? 谁能帮助我完成这项工作?

*Note that I do understand the job will only work for one playerid that is designated in the job. *请注意,我确实了解这项工作仅适用于工作中指定的一个玩家ID。

You don't need to write a cron job for every day of the week. 您不需要在一周的每一天都编写一份cron作业。 Instead of a 0 or 1 in your cron job example you can use an asterisk (*) which will mean all. 在您的cron作业示例中,您可以使用星号(*)代替全部,而不是0或1。 So an asterisk on the second column (hour) would mean run it every hour. 因此,第二列(小时)上的星号表示每小时运行一次。 An asterisk on the first column (minute) would run it every minute. 第一列(分钟)上的星号会每分钟运行一次。

The cron job would look like this: Cron作业如下所示:

* * * * * php -f /[path to dir]/updateplayer.php [playerid]

For the second part, you can pass arguments to the PHP script although you need to fetch them through the $argv variable instead of $_GET. 对于第二部分,您可以将参数传递给PHP脚本,尽管您需要通过$ argv变量而不是$ _GET来获取参数 In the example I provided you would fetch the player id via $argv[1] 在我提供的示例中,您将通过$argv[1]获取玩家ID。

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

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