0

Using cronjob in yii1

This article will explain How to Using cronjob in yii1 to making a file every minutes.

1) First, create a file in /protected/commands/CronjobCommand.php
run php from console of yii

2. Copy the following script :

<?php
class CronjobCommand extends CConsoleCommand{

	public function actionCreateFile(){

		$new_file 	= date('Y-m-d H:i:s') . '.txt';
		$dir		= "/var/www/html/yii1/cron/" . $new_file;
		$f 			= fopen($dir, 'w');

		fclose($f);
	}
}

3) Then, create a folder “cron” inside /yii1/. This folder will be used to store the file.

create folder cron

4) Now, trying to run “CronjobCommand.php” from console.
Open your terminal, type :

 cd /var/www/html/yii1/protected/

To run CronjobCommand.php via console, type :

php yiic Cronjob CreateFile

run php yii from console terminal

We will see a new file inside “cron” folder :
cronjob yii5) All right, now we will make cronjob.  Type below in your terminal :

crontab -e

The following script will be execute CronjobCommand.php every minutes :

* * * * * php /var/www/html/yii1/protected/yiic Cronjob CreateFile

execute yii php via cronjob

Then save (ctrl + x)

Wait every minutes, you can see like this :
Screenshot from 2015-08-02 21:11:19

Ambar Hasbiyatmoko

Hello, I'm web developer. Passionate about programming, web server, and networking.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.