gpt4 book ai didi

php - 如何从 php 脚本设置 cron 作业?

转载 作者:可可西里 更新时间:2023-11-01 12:33:46 25 4
gpt4 key购买 nike

我是 cron 工作的新手。我研究了一些关于 cron 作业的基础知识。我可以使用 cron 选项卡调用 php。通过在/etc/crontab 中使用以下命令

  10 *    * * *   root    /usr/bin/php /var/www/PATH TO SCRIPT/email.php 

在 email.php 中我有以下代码

 #!/usr/bin/php
<?php
mail ("examplemail@mail.com", "Cron Successful Public HTML!","Hello World from mycron.php!");
?>

我每 10 分钟收到一封邮件。但我需要知道有没有办法从 php 调用 cron 作业(从 php 调用 cron)我通过冲浪得到了一些想法,但我无法弄清楚完成我的工作的确切方法。这是我用来通过使用 php 添加作业的代码

   exec('echo -e "crontab -e \n2 * * * * /usr/bin/php /var/www/PATH TO THE SCRIPT/crontest1.php" ');

它不适合我。谁能指导我如何从 php 调用或添加 cron。所以我可以通过执行 php 文件发送邮件,并且可以更改 php 文件本身的时间间隔。

最佳答案

来自 How can I set cron job through PHP script通过 Nick Clark :

This will add a script that runs every day at 9:30am.

exec('echo -e "`crontab -l`\n30 9 * * * /path/to/script" | crontab -');

You may run into problems with permissions if you are running this script from a web server. To get around this, I would suggest a different approach.

Here is one possible solution. Create a list of scripts that need to be run. You can save this in a text file or in a database. Create a script to read this list and run it every minute or every 5 minutes (using a cronjob). Your script will need to be smart enough to decide when to run the list of scripts and when to simply exit.

你可以这样做..

其他链接:

Install a cron job with a php script

How to invoke cron job from php script?

关于php - 如何从 php 脚本设置 cron 作业?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11612910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com