DEPRECATED - Cronjob Manager plugin for Craft CMS
Craft plugin to programmatically manage GNU/Linux cronjobs., (*1)
Deprecated:
With the release of Craft 3 on 4-4-2018, this plugin has been deprecated. You can still use this with Craft 2 but you are encouraged to use (and develop) a Craft 3 version. At this moment, I have no plans to do so., (*2)
Features:
- Deal with your cronjobs in Craft.
- Create new cronjobs.
- Update existing cronjobs.
- Manage cronjobs of others users than runtime user using some sudo tricks (see below).
Requirements:
-
crontab
command-line utility (should be already installed in your distribution).
-
sudo
, if you want to manage crontab of another user than runtime user without running into right issues (see below)
Installation:
The library can be installed using Composer., (*3)
composer require boboldehampsink/cronjob
Usage:
The plugin is composed of three models:, (*4)
-
CronjobModel
is an entity model which represent a cronjob.
-
Cronjob_RepositoryModel
is used to persist/retrieve your jobs.
-
Cronjob_AdapterModel
abstracts raw crontab read/write.
Instanciate the repository:
In order to work, the Cronjob_RepositoryModel
needs an instance of Cronjob_AdapterModel
which handles raw read/write against the crontab., (*5)
$crontabRepository = new Cronjob_RepositoryModel(new Cronjob_AdapterModel());
Create a new cronjob and persist it into the crontab:
Suppose you want to create an new job which consist of launching the command "df >> /tmp/df.log" every day at 23:30. You can do it in two ways., (*6)
$cronjob = new CronjobModel();
$cronjob->minutes = '30';
$cronjob->hours = '23';
$cronjob->dayOfMonth = '*';
$cronjob->months = '*';
$cronjob->dayOfWeek = '*';
$cronjob->taskCommandLine = 'df >> /tmp/df.log';
$cronjob->comments = 'Logging disk usage'; // Comments are persisted in the crontab
- From raw cron syntax string using a factory method:
$cronjob = CronjobModel::createFromCrontabLine('30 23 * * * df >> /tmp/df.log');
You can now add your new cronjob in the crontab repository and persist all changes to the crontab., (*7)
$crontabAdapter = new Cronjob_AdapterModel();
$crontabRepository = new Cronjob_RepositoryModel($crontabAdapter);
$crontabRepository->addJob($cronjob);
$crontabRepository->persist();
Find a specific cronjob from the crontab repository and update it:
Suppose we want to modify the hour of an already existing cronjob. Finding existings jobs is made using some regular expressions. Search in made against the entire crontab line., (*8)
$results = $crontabRepository->findJobByRegex('/Logging\ disk\ usage/');
$cronjob = $results[0];
$cronjob->hours = '21';
$crontabRepository->persist();
Remove a cronjob from the crontab:
You can remove a job like this :, (*9)
$results = $crontabRepository->findJobByRegex('/Logging\ disk\ usage/');
$cronjob = $results[0];
$crontabRepository->removeJob($cronjob);
$crontabRepository->persist();
Note: Since cronjobs are internally matched by reference, they must be previously obtained from the repository or previously added., (*10)
Work with the crontab of another user than runtime user:
This feature allow you to manage the crontab of another user than the user who launched the runtime. This can be useful when the runtime user is www-data
but the owner of the crontab you want to edit is your own linux user account., (*11)
To do this, simply pass the username of the crontab owner as parameter of the Cronjob_AdapterModel
constructor. Suppose you are www-data
and you want to edit the crontab of user bobby
:, (*12)
$crontabAdapter = new Cronjob_AdapterModel('bobby');
$crontabRepository = new Cronjob_RepositoryModel($crontabAdapter);
Using this way you will propably run into user rights issue.
This can be resolved by editing your sudoers file using 'visudo'.
If you want to allow user www-data
to edit the crontab of user bobby
, add this line:, (*13)
www-data ALL=(bobby) NOPASSWD: /usr/bin/crontab
which tells sudo to not ask for password when you call crontab
of user bobby
, (*14)
Now, you can have access to the crontab of user bobby
like this :, (*15)
$crontabAdapter = new Cronjob_AdapterModel('bobby', true);
$crontabRepository = new Cronjob_RepositoryModel($crontabAdapter);
Note the second parameter true
of the Cronjob_AdapterModel
constructor call. This boolean tell the Cronjob_AdapterModel
to use 'sudo' internally to read/write the crontab., (*16)
Enable or disable a cronjob
You can enable or disable your cron jobs by setting the "enabled" attribute of a CronjobModel
object accordingly:, (*17)
$crontabJob->enabled = false;
This will have the effect to prepend your cronjob by a "#" in your crontab when persisting it., (*18)
Todo
- Create ElementType to manage cronjobs from the CP
Changelog
0.1.1
0.1.0
- Initial release that allows you to manage cronjobs programmatically
Credits
Based on TiBeN/CrontabManager, (*19)