如何从PHP脚本设置cron作业? [英] how to set cron job from php script?

查看:114
本文介绍了如何从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.com, 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.com", "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作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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