代码之家  ›  专栏  ›  技术社区  ›  Robert Hurst Patrick Ryan

使用php脚本安装cron作业

  •  6
  • Robert Hurst Patrick Ryan  · 技术社区  · 16 年前

    我正在开发一个需要使用Cron的web应用程序。我想通过像Wordpress这样的自动安装过程来简化安装。在设置Cron之前,我在编写安装脚本方面没有问题。请告诉我我是否能做到这一点。

    1 回复  |  直到 16 年前
        1
  •  9
  •   Tatu Ulmanen    16 年前

    您只需创建cron文件,然后使用exec设置该cron:

    $cron_file = 'cron_filename';
    // Create the file
    touch($cron_file); 
    // Make it writable
    chmod($cron_file, 0777); 
    // Save the cron
    file_put_contents($cron_file, '* * * * * your_command'); 
    // Install the cron
    exec('crontab cron_file');
    

    这要求运行PHP的用户有权创建crontab。默认情况下,此cron文件将替换该用户的任何其他cron,因此请确保询问用户是否要应用cron。还要确保正在写入crontab文件的文件夹是可写的。

    推荐文章