Install a cron job with a php script

后端 未结 1 507
轻奢々
轻奢々 2020-12-03 06:10

I\'m developing a web application that requires the use of Cron. I\'d like to make it easy to setup with an auto install process like Wordpress. I have no problems writing t

相关标签:
1条回答
  • 2020-12-03 06:47

    You just have to create the cron file, then use exec to set up that 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');
    

    This requires that the user which PHP is run under has the right to make crontabs. This cron file will by default replace any other crons for that user, so make sure to ask the user if he wants to apply the cron. Also make sure the folder you're writing the crontab file in is writable.

    0 讨论(0)
提交回复
热议问题