如何从PHP脚本设置cron作业?

编程入门 行业动态 更新时间:2024-10-15 18:26:52
本文介绍了如何从PHP脚本设置cron作业?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是cron工作的新手。我研究了一些有关cron工作的基础知识。我可以使用cron选项卡调用php。通过在/ etc / crontab中使用以下命令

10 * * * * root / usr / bin / php / var / www /脚本路径/email.php

在email.php中,我有以下代码

#!/ usr / bin / php <?php 邮件( examplemail@mail, Cron成功的公共HTML!,来自mycron.php的Hello World!); ?>

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

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

它对我不起作用。谁能指导我如何从php调用或添加cron。所以tat我可以通过执行php文件&发送邮件能够改变PHP文件本身的时间间隔。

解决方案

来自如何通过PHP脚本设置尼克·克拉克:

这将添加一个每天上午9:30运行的脚本。

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

如果从Web服务器运行此脚本,可能会遇到权限问题。为了解决这个问题,我建议采用另一种方法。

这里是一种可能的解决方案。创建需要运行的脚本列表。您可以将其保存在文本文件或数据库中。创建一个脚本来读取此列表,并每分钟或每5分钟运行一次(使用cronjob)。您的脚本必须足够聪明,以决定何时运行脚本列表以及何时仅退出脚本。

您可以这样做。.

其他链接:

使用php脚本安装cron作业

如何从php脚本调用cron作业?

i am new to cron job . i studied some basics about cron job . i can able to call php using cron tab. By using following command in /etc/crontab

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

In email.php i have following code

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

For every 10mins i am getting mail. But i need to know is there any way to call cron job from php ( invoke cron from php) i get some idea by surfing but i am not able to figure out the exact way to do my job . Here is code which i used to add job by using php

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

Its not working for me. can anyone please guide me how to call or add cron from php. so tat i can send mail by executing php file & can able change time interval in php file itself.

解决方案

From How can I set cron job through PHP script by 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.

You can do like this..

Other links:

Install a cron job with a php script

How to invoke cron job from php script?

更多推荐

如何从PHP脚本设置cron作业?

本文发布于:2023-11-28 18:16:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1643447.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:作业   脚本   PHP   cron

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!