Yii Linux Automatic execution script

Source: Internet
Author: User

To configure a custom execution script in a system developed today with Yii


1. Configure the components that are needed in the product/config/console.php, like database connections


' DB ' =>array (
' connectionString ' = ' mysql:host=localhost;dbname=testdrive ',
' Emulateprepare ' = true,
' Username ' = ' root ',
' Password ' = ',
),
2. Inherit Cconsolecommand Write your own command class.


Yii provides two ways to do this, if you perform a single task, write directly in the Run method, and the other is similar to the action (action) in the controller (ACTIONXXX), with the addition of the front, the framework/ Many examples are available under the console for our reference. For example, Chelpcommand uses run directly:


Class Chelpcommand extends Cconsolecommand
{
/**
* Execute the action.
* @param array $args command line parameters specific for this command
*/
Public function run ($args)
{
$runner = $this->getcommandrunner ();
$commands = $runner->commands;
if (isset ($args [0]))
$name =strtolower ($args [0]);
if (!isset ($args [0]) | |!isset ($commands [$name]))
{
if (!emptyempty ($commands))
{
echo "Yii command Runner (based on Yii V"). Yii::getversion (). ") \ n ";
echo "Usage:". $runner->getscriptname (). "<command-name> [Parameters ...] \ n ";
echo "\nthe following commands is available:\n";
$commandNames =array_keys ($commands);
Sort ($commandNames);
Echo '-'. Implode ("\ n-", $commandNames);
echo "\n\nto see individual command help, use the following:\n";
echo "". $runner->getscriptname (). "Help <command-name>\n";
}else{
echo "No available commands.\n";
echo "Please define them under the following directory:\n";
echo "\ T". Yii::app ()->getcommandpath (). " \ n ";
}
}else
echo $runner->createcommand ($name)->gethelp ();
}
/**
* provides the command description.
* @return String the command description.
*/
Public Function Gethelp ()
{
Return Parent::gethelp (). ' [Command-name] ';
}
}
Next use the action to execute the cleanup command


Class Cleanupcommand extends Cconsolecommand {

Private $sessionTableName = ' it_common_session ';

/**
* Automatically perform cleanup session every 2 minutes.
*/
Public Function actionsession () {
$now = time ();
$sql = "DELETE from {$this->sessiontablename} WHERE lastactivity < $now";
$command = Yii::app ()->db->createcommand ($sql);
$command->execute ();
}

/**
* Clean up the system cache every day 7:30
*/
Public Function Actioncache () {
Yii::app ()->cache, flush ();
Yii::app ()->dbcache, flush ();
Yii::app ()->fcache, flush ();
}

/**
* Clear Upload Temporary files
*/
Public Function Actiontempfiles () {
$dir = Yii::getpathofalias (' site.frontend.www.uploads.temp '). ' /‘;
foreach (Glob ($dir. ' *. * ') as $v) {
Unlink ($v);
}
}
}
3. Using the Linux command, VI crontab-e turns on automatic tab, adding one line


2 * * * php/path/to/console.php cleanup XXX Here's the parameter put action >>/path/to/logfile
console.php to create and import file siblings in the Yii application directory:


<?php
$YIIC =dirname (__file__). ' /protected/yiic.php ';
$config =dirname (__file__). ' /protected/config/console.php ';
@putenv (' yii_console_commands= '. DirName (__file__). ' /protected/commands ');

Require_once ($YIIC);
The above means that every night two o'clock automatic cleanup tasks, simple steps to complete the powerful task of automation, is not simple enough?


Problems:


1). If crontab is not automatically executed, carefully check if your grammar is correct.


2). Determine if the Protected/yiic file has execute permissions, if you do not use the command chmod +x YIIC authorization

Yii Linux Automatic execution script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.