如何使用 PHP 创建 cron 作业? [英] How to create cron job using PHP?

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

问题描述

我是使用 cron 作业的新手.我什至不知道怎么写.我试图从互联网上搜索,但我仍然不太明白.我想创建一个 cron 作业,每分钟执行一次我的代码.我正在使用 PHP 来创建它.它不起作用.

示例

run.php (每分钟执行一次的代码)

cron.php

假设这两个文件在同一个文件夹中.

是我做错的代码吗?如果有错误,请告诉我如何修复.

解决方案

这是我目前发现的对 PHP 代码最好的解释:

例如,如果有人想在每个月的第一天的凌晨 12 点安排一项任务,它看起来像这样:

0 0 1 * * home/path/to/command/the_command.sh

如果我们想安排一个任务在每周六早上 8 点 30 分运行,我们可以这样写:

30 8 * * 6 home/path/to/command/the_command.sh

还有许多运算符可用于进一步自定义时间表:

Commas 用于为任何 cron 列创建逗号分隔的值列表.破折号用于指定值的范围.星号用于指定 'all' 或 'every' 值

访问完整文章的链接,它解释了:

  1. 如果您想手动输入/编辑 cronjob 的格式是什么.
  2. 如何使用 PHP 和 SSH2 库来验证用户身份,您将编辑哪个 crontab.
  3. 完整的 PHP 类,包含用于身份验证、编辑和删除 crontab 条目的所有必要方法.

I'm new to using cron job. I don't even know how to write it. I have tried to search from internet, but I still don't understand it well. I want to create a cron job that will execute my code every minute. I'm using PHP to create it. It is not working.

Example

run.php (Code that will be executed every minute)

<?php

echo "This code will run every minute";

?>

cron.php

<?php

$path = dirname(__FILE__);
$cron = $path . "/run.php";
echo exec("***** php -q ".$cron." &> /dev/null");

?>

Suppose that these two files are in the same folder.

Is the code that I did wrong? If wrong, please kindly tell me how to fix it.

解决方案

This is the best explanation with code in PHP I have found so far:

http://code.tutsplus.com/tutorials/managing-cron-jobs-with-php--net-19428

In short:

Although the syntax of scheduling a new job may seem daunting at first glance, it's actually relatively simple to understand once you break it down. A cron job will always have five columns each of which represent a chronological 'operator' followed by the full path and command to execute:

* * * * * home/path/to/command/the_command.sh

Each of the chronological columns has a specific relevance to the schedule of the task. They are as follows:

Minutes represents the minutes of a given hour, 0-59 respectively.
Hours represents the hours of a given day, 0-23 respectively.
Days represents the days of a given month, 1-31 respectively.
Months represents the months of a given year, 1-12 respectively.
Day of the Week represents the day of the week, Sunday through Saturday, numerically, as 0-6 respectively.

So, for example, if one wanted to schedule a task for 12am on the first day of every month it would look something like this:

0 0 1 * * home/path/to/command/the_command.sh

If we wanted to schedule a task to run every Saturday at 8:30am we'd write it as follows:

30 8 * * 6 home/path/to/command/the_command.sh

There are also a number of operators which can be used to customize the schedule even further:

Commas is used to create a comma separated list of values for any of the cron columns.
Dashes is used to specify a range of values.
Asterisksis used to specify 'all' or 'every' value

Visit the link for the full article, it explains:

  1. What is the format of the cronjob if you want to enter/edit it manually.
  2. How to use PHP with SSH2 library to authenticate as the user, which crontab you are going to edit.
  3. Full PHP class with all necessary methods for authentication, editing and deleting crontab entries.

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

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