douyi8732 2012-07-23 12:52
浏览 32

如何从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 
2 * * * * /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.

  • 写回答

3条回答 默认 最新

  • 「已注销」 2012-07-23 12:55
    关注

    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`
    30 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?

    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线