pnixx / crontab
提供清晰语法来编写和部署cron任务
1.0.3
2024-09-04 12:44 UTC
Requires
- php: >=7.1
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-18 13:04:41 UTC
README
PHP的Crontab提供了编写和部署cron任务的清晰语法(灵感来自whenever)。
安装
$ composer require pnixx/crontab
使用
use PNixx\Crontab\Crontab; use PNixx\Crontab\Job; //Initialize constructor $crontab = new Crontab('example.com', '/path/to/example.com'); //Add job for run every minute $job = new Job('bin/console hello'); $crontab->add($job); //Add job for run hourly $job = new Job('bin/console update'); $job ->setTime(Job::HOURLY) ->setLogFile('logs/execute.log'); $crontab->add($job); //Add job for run custom time $job = new Job('rm -Rf /var/cache'); $job->setTime('15 * * * *'); $crontab->add($job); //Add job for run every two minutes $job = new Job('echo "Hello World!"'); $job->setMinute('*/2'); $crontab->add($job); //Update crontab $crontab->update();
将结果追加或替换到您的crontab中
#===BEGIN Crontab for project: example.com
* * * * * cd /path/to/example.com && bin/console hello
0 * * * * cd /path/to/example.com && bin/console update >> /path/to/example.com/logs/execute.log
15 * * * * cd /path/to/example.com && rm -Rf /var/cache
*/2 * * * * cd /path/to/example.com && echo "Hello World!"
#===END Crontab for project: example.com
Capistrano\Symfony集成
请参考Capistrano::Symfony文档插件。
用于生成crontab的Symfony 3命令类
use PNixx\Crontab\Crontab; use PNixx\Crontab\Job; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class CrontabUpdateCommand extends ContainerAwareCommand { public function configure() { $this->setName('crontab:update'); $this->setDescription('Update all cron tasks for project'); } protected function execute(InputInterface $input, OutputInterface $output) { $root_path = realpath($this->getContainer()->get('kernel')->getRootDir() . '/..'); //Initialize constructor crontab for current environment $crontab = new Crontab($this->getContainer()->get('kernel')->getEnvironment(), $root_path); //Add your jobs $crontab->add(new Job('echo "Hello World!"')); //Update $crontab->update(); } }
将以下内容添加到deploy.rb
中,适用于Capistrano '~> 3.5'
namespace :deploy do task :crontab do on roles(:db) do invoke 'symfony:console', 'crontab:update', '--no-interaction' end end end after 'deploy:published', 'deploy:crontab'