Use PHP to create, edit and delete crontab jobs?

前端 未结 12 1316
余生分开走
余生分开走 2020-11-22 06:54

Is it possible to use PHP to create, edit and delete crontab jobs?

I know how to list the current crontab jobs of the Apache user:

$output = shell_ex         


        
12条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 07:02

    We recently prepared a mini project (PHP>=5.3) to manage the cron files for private and individual tasks. This tool connects and manages the cron files so you can use them, for example per project. Unit Tests available :-)

    Sample from command line:

    bin/cronman --enable /var/www/myproject/.cronfile --user www-data
    

    Sample from API:

    use php\manager\crontab\CrontabManager;
    
    $crontab = new CrontabManager();
    $crontab->enableOrUpdate('/tmp/my/crontab.txt');
    $crontab->save();
    

    Managing individual tasks from API:

    use php\manager\crontab\CrontabManager;
    
    $crontab = new CrontabManager();
    $job = $crontab->newJob();
    $job->on('* * * * *');
    $job->onMinute('20-30')->doJob("echo foo");
    $crontab->add($job);
    $job->onMinute('35-40')->doJob("echo bar");
    $crontab->add($job);
    $crontab->save();
    

    github: php-crontab-manager

提交回复
热议问题