如何使用PHP创建cron作业? [英] How to create cron job using PHP?
问题描述
我刚开始使用cron job。我甚至不怎么写。我试图从互联网搜索,但我还是不明白。我想创建一个cron作业,每分钟执行我的代码。我使用PHP来创建它。
run.php
/ strong>(每分钟执行的代码) <?php
echo这段代码将每分钟运行一次;
?>
cron.php
<?php
$ path = dirname(__ FILE__);
$ cron = $ path。 /run.php;
echo exec(***** php -q。$ cron。&> / dev / null);
?>
假设这两个文件在同一个文件夹中。
是我的代码错了吗?
提前感谢。
以同样的方式,你试图运行cron.php,你可以运行另一个PHP脚本。你必须通过CLI界面这样做。
#!/ usr / bin / env php
< ;?php
#这个文件会是'/usr/local/bin/run.php'
// code
echo这是从CRON运行
然后,向crontab添加一个条目:
* * * * * / usr / bin / php -f /usr/local/bin/run.php&> / dev / null
如果run.php脚本具有可执行权限, crontab,没有/ usr / bin / php部分。脚本中的'env php'部分将找到适当的程序来实际运行PHP代码。所以对于'executable'版本添加可执行文件的权限:
chmod + x / usr / local / bin / php
,然后将以下条目添加到crontab中:
* * * * * /usr/local/bin/run.php&> / dev / null
I'm new to using cron job. I don't even 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.
Thanks in advance.
解决方案In the same way you are trying to run cron.php, you can run another PHP script. You will have to do so via the CLI interface though.
#!/usr/bin/env php <?php # This file would be say, '/usr/local/bin/run.php' // code echo "this was run from CRON"
Then, add an entry to the crontab:
* * * * * /usr/bin/php -f /usr/local/bin/run.php &> /dev/null
If the run.php script had executable permissions, it could be listed directly in the crontab, without the /usr/bin/php part as well. The 'env php' part in the script would find the appropriate program to actually run the PHP code. So for the 'executable' version add executable permission to the file:
chmod +x /usr/local/bin/run.php
and then add the following entry into crontab:
* * * * * /usr/local/bin/run.php &> /dev/null
这篇关于如何使用PHP创建cron作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!