Skip to content

Instantly share code, notes, and snippets.

@ryun
Created April 20, 2017 00:03
Show Gist options
  • Save ryun/ba2375e595346bfaad5959b5e3d23fa3 to your computer and use it in GitHub Desktop.
Save ryun/ba2375e595346bfaad5959b5e3d23fa3 to your computer and use it in GitHub Desktop.
Add and remove cron entry for scheduler.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
class CronManagerCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'schedule:cron {action}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Add and remove cron entry for scheduler.';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$command = 'crontab -l | grep -v \'schedule:run\' | crontab -';
switch ($this->argument('action')) {
case 'add':
$command = $command.' && (crontab -l ; echo "* * * * * php ${PWD}/artisan schedule:run >> /dev/null 2>&1") | crontab -';
$message = 'Added crontab entry for scheduler.';
break;
case 'rem':
case 'remove':
$message = 'Removed crontab entry for scheduler.';
break;
default:
$this->info('Available actions are [add|remove]');
exit();
}
$process = new Process($command);
$process->run();
if ( ! $process->isSuccessful()) {
throw new ProcessFailedException($process);
}
$this->line($process->getOutput());
$this->info($message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment