sharkom / yii2-cron
Create Cron jobs from browser, and look that run logs
Requires
- dragonmantank/cron-expression: ^1.2
- kartik-v/yii2-grid: @dev
- sharkom/yii2-cronjob-widget: ^1
- symfony/process: ^5.0
- yiisoft/yii2: ~2.0.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-13 09:09:14 UTC
README
Create Cron jobs from browser, and look that run logs
Forked from vasadibt/yii2-cron with some modifications:
- Now it can run every kind of script, not only Yii2 console commands
- Added capability to have a log file in addition to DB logs
- Removed the runquick capabilities
ChangeLog 27/Feb/2023
- Add Manual Run button on cronjob page
- Add auto purge logs (use module param purge_log_interval | default 3 months) - removed on 2/Nov/2026, see below
- Fix some graphics in logs pages
ChangeLog 5/May/2023
- Added execution error mail notifications
- Added auto unlock mail notifications
- Added module param sendNotifications
ChangeLog 2/Nov/2026
- Removed the purge of commands_spool from every cron/cron/run (purge_log_interval)
- New command: php yii cron/maintenance/purge-spool [--days=14] [--optimize=1] [--max-seconds=1200]
- deletes in blocks of 500 only finished rows (completed <> 0) older than the retention
- removes the command log files only inside @runtime/logs and only when no remaining row uses them
- with --optimize=1 rebuilds the table online (ALGORITHM=INPLACE, LOCK=NONE) above the thresholds
- Module param purge_spool_days (days, default 14) replaces purge_log_interval (months, converted x 30 with a deprecation warning)
- Migration migrations/m261102_160100_schedule_purge_spool.php: nightly job (job dispatcher) or cron_job "45 2 * * *"
- Without the maintenance classes of sharkom/yii2-dev-helper (sharkom\devhelper\maintenance, e.g. NormaFlow) the command falls back to a simple purge (same protection, blocks of 500, no file removal, no optimization) and cron/cron/run runs it at most once a day
ChangeLog 4/Nov/2026
- The spooler rotates the command log before opening it: above log_rotate_max_mb (module param, MB, default 50; 0 disables) the file is moved and gzip-compressed to .1.gz, older copies shift up to .<log_rotate_keep>.gz (default 5) and the oldest is deleted (services/LogRotator.php, lock file .lock; on errors the command keeps writing)
- Migration migrations/m261104_120000_cron_job_log_in_runtime.php: cron_job with logfile in /var/log/ get an empty logfile (@runtime/logs/command_cron_job_.log); down does not restore (up prints the UPDATEs to restore by hand)
- purge-spool: files still used are compared by realpath, files modified in the last 24 hours are kept, removal is rename -> recheck -> unlink (or rename back), rotated copies *.N.gz are never removed
Note: the full purge (log files and online table rebuild) needs the classes sharkom\devhelper\maintenance of sharkom/yii2-dev-helper (suggested in composer.json, not required); without them the simple purge is used.
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist sharkom/yii2-cron "*"
or add
"sharkom/yii2-cron": "*"
to the require section of your composer.json file.
Migration
Run the following command in Terminal for database migration:
yii migrate/up --migrationPath=@sharkom/cron/migrations
Or use the namespaced migration (requires at least Yii 2.0.10):
// Add namespace to console config: 'controllerMap' => [ 'migrate' => [ 'class' => 'yii\console\controllers\MigrateController', 'migrationPath' => [ '@sharkom/cron/migrations', ], ], ],
Then run:
yii migrate/up
Web Application Config
Turning on the Cron Job Manager Module in the web application:
Simple example:
'modules' => [ 'cron' => [ 'class' => 'sharkom\cron\Module', 'params'=>[ 'sendNotifications'=>true, ] ], ],
Console Application Config
Turning on the Cron Job Manager Module in the console application:
Simple example:
'modules' => [ 'cron' => [ 'class' => 'sharkom\cron\Module', 'params'=>[ 'sendNotifications'=>true, ] ], ],
Schedule Config
Set the server schedule to run the following command
On Linux:
Add to the crontab with the user who you want to run the script (possibly not root) with the crontab -e command or by editing the /etc/crontab file
* * * * * <your-application-folder>/yii cron/cron/run 2>&1
On Windows:
Open the task scheduler and create a new task
###Email notifications
In order to recieve email notifications for execution errors you need to config:
- The module param sendNotifications to true (check Web App and Console App configuration in this readme)
- Set in common/config/params.php the parameters:
- senderEmail
- NotificationsEmail
- Configure the mailer in common/config/main-local.php
return [ 'senderEmail'=>'notifications@yourapp.net', 'NotificationsEmail'=>'notifications@yourapp.net', ];
'mailer' => [ 'class' => 'yii\swiftmailer\Mailer', // send all mails to a file by default. You have to set // 'useFileTransport' to false and configure a transport // for the mailer to send real emails. 'useFileTransport' => false, 'transport' => [ 'class' => 'Swift_SmtpTransport', 'encryption' => 'tls', 'host' => 'your smtp relay', 'port' => '25', 'username' => 'your user', 'password' => 'your password', ], ],
.