你如何设置cron任务? [英] How do you set up cron task?

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

问题描述

我从来没有使用过CRON,但是我想使用CRON,以便能够为php脚本执行计划作业。 php脚本被称为inactivesession.php,在php脚本中是这个代码:

 <?php 

include('connect.php');


$ createDate = mktime(0,0,0,10,25,date(Y));
$ selectedDate = date('d-m-Y',($ createDate));

$ sql =UPDATE Session SET Active =?WHERE DATE_FORMAT(SessionDate,'%Y-%m-%d')< =?
$ update = $ mysqli-> prepare($ sql);
$ update-> bind_param(is,0,$ chosenDate);
$ update-> execute();

?>

我想要做的是,当上述日期到达(10月25日),我想要php脚本执行上面的UPDATE语句。但我的问题是,如何使用CRON为了这样做?



我使用的服务器是大学的服务器helios,需要设置CRON



我从来没有使用过CRON,所以你可以解释一下我怎么可以设置CRON为上面的例子与我使用的服务器?



感谢

解决方案

方法1:使用php crontab



就像你调用你的shell脚本一样(如我们的crontab 15示例文章中所示),使用php可执行文件,并从crontab调用php脚本



要每1小时执行myscript.php,请执行以下操作:



crontab - e



  00 * * * * / usr / local / bin / php /home/john/myscript.php方法2:使用crontab中的URL运行php脚本






如果您的php脚本可以使用URL调用,您可以使用lynx,curl或wget来设置您的crontab,如下所示。



下面的脚本通过使用lynx文本浏览器调用URL来执行php脚本(每小时)。 Lynx文本浏览器默认在交互模式下打开一个URL。但是,如下所示,lynx命令中的-dump选项将URL的输出转储到标准输出。

  00 * * * * lynx -dump http://www.thegeekstuff.com/myscript.php 

下面的脚本通过使用CURL调用URL来执行php脚本(每5分钟)。 Curl默认显示标准输出中的输出。使用curl -o选项,您还可以将脚本的输出转储到临时文件,如下所示。

  * / 5 * * * * / usr / bin / curl -o temp.txt http://www.thegeekstuff.com/myscript.php 

以下脚本通过使用WGET调用URL来执行php脚本(每10分钟一次)。 -q选项指示完全模式。 -O temp.txt表示输出将发送到临时文件。

  * / 10 * * * * / usr / bin / wget -q -O temp.txt http://www.thegeekstuff.com/myscript.php 


I have never used CRON before but I want to use CRON in order to be able to perform schedule jobs for a php script. The php script is called "inactivesession.php" and in the php script is this code:

<?php

include('connect.php');


$createDate = mktime(0,0,0,10,25,date("Y"));
$selectedDate =  date('d-m-Y', ($createDate));

$sql = "UPDATE Session SET Active = ? WHERE DATE_FORMAT(SessionDate,'%Y-%m-%d' ) <= ?";                                         
$update = $mysqli->prepare($sql);
$update->bind_param("is", 0, $selectedDate);
$update->execute();

?>

Wht I want to do is that when the above date is reached (25th Oct), I want the php script to perform the UPDATE statement above. But my question is that how do I use CRON in order to do this?

The server I am using is the university's server known as helios, does CRON need to be set up in helios, (do I have to call the admin for this) or is it something else which uses CRON.

I have never used CRON before so can you explain to me how CRON can be set up for the example above with the server I am using?

Thanks

解决方案

Method 1: Execute the script using php from the crontab

Just like how you call your shell script (As show in our crontab 15 examples article), use the php executable, and call the php script from your crontab as shown below.

To execute myscript.php every 1 hour do the following:

crontab -e

00 * * * * /usr/local/bin/php /home/john/myscript.php

Method 2: Run the php script using URL from the crontab

If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.

The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.

00 * * * * lynx -dump http://www.thegeekstuff.com/myscript.php

The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the "curl -o" option, you can also dump the output of your script to a temporary file as shown below.

*/5 * * * * /usr/bin/curl -o temp.txt http://www.thegeekstuff.com/myscript.php

The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The "-O temp.txt" indicates that the output will be send to the temporary file.

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.thegeekstuff.com/myscript.php

这篇关于你如何设置cron任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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