I am working on yii2
. I have a create function which I want to run in the background after every 24 hours.
public function actionCreate()
{
$model = new MeterPing();
// CURL code to get results from web-api
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
Also in this function, I will be fetching some records from a web-API
. But my main concern is to run the above function in the background after every 24 hours.
Update 1
As per suggestion was given I have created a controller in console\controller
namespace console\controllers;
use yii\console\Controller;
class TestController extends Controller {
public function actionIndex() {
echo "cron service runnning";
}}
Now, for testing it I followed this link and created a run service using cmd
with following parameters
Now, by following as per suggested I have tried each step. The .bat
file contains
@Echo off
title My Cron Job
:: See title at the top
php E:\xampp\htdocs\inventory-web\yii test
pause
After saving the file and adding it to the task scheduler. I tried to run it from this interface. And I did see a cmd opening and showing the message.
How can I do it? Any help would be highly appreciated.
You can use Task Scheduler for windows
- Create a
.bat
file which should contain the command for running your Yii console app controller/action
if you create a controller class TestController
that extends yii\console\controller
and add a create
action, to run that action from Yii console application you would type ./yii test/create
on the project root using gitBash or console, so we will add this command to .bat
file by providing the full path to the project_root/yii
which is the Yii console bootstrap file. This is the main thing to run the job as part of Yii controller action code.
open notepad and copy below code in it an save as .bat
with name my-cronjob.bat
@Echo off
title My Cron Job
:: See title at the top
php F:\xampp\htdocs\my-project\yii test/create
pause
NOTE: make sure you have php
in Windows Path Variable
or you should provide the complete path to php.exe
in the above .bat
file
Select Trigger time
Daily
Select time to execute the task
Select
ActionStart a programme
Now select the
my-cronjob.batfile and press Next and Finish
Now go to task manager and select
Task Scheduler Libraryand right click the cronjob you just created and open properties.
- Select
Run with highest privileges
NOTE: you can select to run the cronjob if the user is logged-in or not, by default it only runs if the user is logged-in you can change that option.
That is it now you can either manually run the task by right-clicking and selecting run and it will run the desired controller/action
specified in the my-cronjob.bat
or wait for it to trigger on the specified time.
For running in background
The above settings will open the command prompt and run the task if you want the scheduler to run the task minimized you should see this link
and update the task settings accordingly. also you can change the last line pause
to Exit
too.
I have tested it before posting the answer here as I have Windows10
OS at my home, so couldn't post the answer without testing from office.
It seems to me that you are looking for a cronjob.
You'll have to create a console cronjob and then add it to your crontab on the server. The above link has a wiki on how to set this up for Yii2.
Example crontab which will run every day at 3 PM:
0 15 * * * /var/www/project/yii daemon/create