Skip to content

Instantly share code, notes, and snippets.

@Shaked
Created January 13, 2018 15:01
Show Gist options
  • Save Shaked/f3245be5f0e7690a968ccd1da729d21e to your computer and use it in GitHub Desktop.
Save Shaked/f3245be5f0e7690a968ccd1da729d21e to your computer and use it in GitHub Desktop.
How I manage my cronjobs using deployer.org
<?php
/**
* * * * * "command to be executed"
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
*/
//unique project key for crontab
set('cron_job_key', get('domain') . 'CRON_JOBS_FROM_DEPLOYMENT');
set('cron_jobs', function () {
return [
'prod' => [
'* 12 * * * sudo -u www-data php ' . get('deploy_path') . '/current/web/console.php do:something', //every 12 hours
],
];
});
desc('Setup cronjobs');
task('deploy:cronjobs', function () {
$stage = get('stage');
$cronJobKey = getCronJobKey();
//delete all cronjobs with unique key
$resetCronJobsFromDeployment = sprintf('crontab -l | grep -v "%s" | crontab -', $cronJobKey);
writeln('Resetting crontab list using key: ' . $cronJobKey);
run($resetCronJobsFromDeployment);
//add all cronjobs with unique key
$cron_jobs = get('cron_jobs');
$crons = [];
writeln('Adding crons');
if (isset($cron_jobs[$stage])) {
foreach ($cron_jobs[$stage] as $cron_job) {
$cron = sprintf('%s #%s', $cron_job, $cronJobKey);
$add = '(crontab -l ; echo "' . $cron . '") | crontab -';
run($add);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment