Cron Jobs in Ubuntu
Cron is a time-based job scheduling. Cron runs in the background and tasks scheduled with cron, referred to as “cron jobs,” are executed automatically, making cron useful for automating maintenance-related tasks.
Commands
crontab -e
: Edit your cron jobs.crontab -l
: List the all your cron jobs.crontab -r
: Delete the current cron jobs.
The schedule component of the syntax is broken down into 5 different fields, which are written in the following order:
Field | Allowed Values |
---|---|
minute | 0-59 |
hour | 0-23 |
Day of the month | 1-31 |
month | 1-12 or JAN-DEC |
Day of the week | 0-6 or SUN-SAT |
Format like this
minute hour day_of_month month day_of_week command_to_run
To define the time you can provide concrete values for
minute (m), hour (h), day of month (dom), month (mon),
and day of week (dow) or use '*' in these fields (for 'any').#
Notice that tasks will be started based on the cron's system
daemon's notion of time and timezones.
Examples
Every minutes
* * * * * php /var/www/html/yiitest2.0.39/yii test
12 * * * *
- Run the command 12 minutes after every hour
0 4 * * *
- Run the command every day at 4:00 AM.
*/15 * * * *
- Run the command every 15 minutes
Cronjob in Yii2
Create console application
In advance template there is already a file yii. And there is no need to run it as php, it is Linux script.
Create cron service command
Create a controller in console/controllers
I have created as TestController.php
<?php
namespace console\controllers;
use yii\console\Controller;
/**
* Test controller
*/
class TestController extends Controller {
public function actionIndex() {
echo "cron service runnning";
}
public function actionMail($to) {
echo "Sending mail to " . $to;
}
}
This controller should be use the console controller name space
use yii\console\Controller;
How to run it
run it as
yii test
No comments:
Post a Comment