PHP code example of pnixx / crontab

1. Go to this page and download the library: Download pnixx/crontab library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pnixx / crontab example snippets


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();


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();
    }
}