Created
April 20, 2012 18:12
-
-
Save tmaiaroto/2430802 to your computer and use it in GitHub Desktop.
Lithium Cron Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This script is meant to be executed via crontab. | |
* It should help you easily set li3 commands as cron jobs. | |
* | |
* You should put this in the root of your application. | |
* However, you don't need to, but you would then need to | |
* pass a --path= option with the path to your app. | |
* This is because the li3 console command must be called | |
* from a specific location. | |
* | |
* Usage Examples: | |
* | |
* This would specify the development environment for some command. | |
* > php /path/to/cron.php --env=development mycommand method | |
* | |
* This would show the li3 help command output. | |
* Note the verbose flag can be useful for testing before setting cron. | |
* > php /path/to/cron.php --verbose help | |
* | |
*/ | |
// While this script shouldn't be able to be accessed from a web browser | |
// because it should sit outside the webroot...This isn't a bad idea. | |
if(php_sapi_name() != 'cli') die('Access denied.'); | |
// Who knows what the li3 console command was doing, this can help prevent | |
// issues with hitting a memory limit. | |
gc_collect_cycles(); | |
// This will parse the args. | |
// --arg=val will be array('arg' => 'val') | |
// arg will be array(0 => 'arg') | |
// --arg will be array('arg' => true) | |
function parseArgs($argv){ | |
array_shift($argv); | |
$out = array(); | |
foreach ($argv as $arg){ | |
if (substr($arg,0,2) == '--'){ | |
$eqPos = strpos($arg,'='); | |
if ($eqPos === false){ | |
$key = substr($arg,2); | |
$out[$key] = isset($out[$key]) ? $out[$key] : true; | |
} else { | |
$key = substr($arg,2,$eqPos-2); | |
$out[$key] = substr($arg,$eqPos+1); | |
} | |
} else if (substr($arg,0,1) == '-'){ | |
if (substr($arg,2,1) == '='){ | |
$key = substr($arg,1,1); | |
$out[$key] = substr($arg,3); | |
} else { | |
$chars = str_split(substr($arg,1)); | |
foreach ($chars as $char){ | |
$key = $char; | |
$out[$key] = isset($out[$key]) ? $out[$key] : true; | |
} | |
} | |
} else { | |
$out[] = $arg; | |
} | |
} | |
return $out; | |
} | |
// Get the passed args. | |
$options = parseArgs($argv); | |
// Specify --env= in the cron, otherwise it will be set to production by default. | |
// Then unset the value so it doesn't get in the way of building the li3 command. | |
$env = (isset($options['env'])) ? $options['env']:'production'; | |
if(isset($options['env'])) { | |
unset($options['env']); | |
} | |
// This script is meant to be in the root of the application. | |
// If it is not, then --path= must be passed with the full path to the app. | |
$li3_app_path = (isset($options['path'])) ? $options['path']:__DIR__; | |
if(isset($options['path'])) { | |
unset($options['path']); | |
} | |
// This can be helpful if you want to test the call before putting it in crontab. | |
// Just call the same command you'd set in cron with --verbose passed and you'll see output. | |
$verbose = (isset($options['verbose'])) ? true:false; | |
if(isset($options['verbose'])) { | |
unset($options['verbose']); | |
} | |
// The command is simply the remainder of the arguments passed. | |
// Technically, the --env and/or --path can be at the beginning or end. | |
$li3_command = join(' ', $options); | |
// Call the command(s) with a subshell. We need ensure li3 is called from the proper directory. | |
$command = "(cd {$li3_app_path} && exec libraries/lithium/console/li3 --env={$env} $li3_command)"; | |
// Do something with the output. | |
if($verbose) { | |
echo `$command`; | |
echo PHP_EOL; | |
} else { | |
$nullResult = `$command > /dev/null &`; | |
} | |
exit(); | |
?> |
Nice!
Note: If you're using Lithium through Composer, then it's likely not going to be at libraries/lithium/...
so you'll need to adjust this script to match the location of the console
command.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This, well commented, simple, and fairly flexible script will help you easily set your li3 commands to run via cron.
Usage Examples
Always specify the full path to the cron.php script of course, these examples assume some "mysite.com" has the application root in a "www" directory. Meaning Lithium would be within /var/www/sites/mysite.com/www/libraries/lithium
If you wanted to run a basic
li3 help
command and see the output.If you wanted to run some hypothetical
users import
command for your development environment. Note that without the --verbose flag, it will stay quiet. You shouldn't use --verbose on the actual entry in crontab.You could also specify all your options at the end of the call too.
The wrapper will use the
production
environment by default so if you were setting this up for a production site, your call might simply look like this.Of course you are likely adding this to cron. So if you're importing some users every 5 minutes, your crontab entry may look something like this.
That should do it! Be sure to test your commands on the command line with --verbose before putting them in cron, that way you'll know if they are working through this wrapper. It's also a good idea to ensure you're editing the crontab with a user that has permission to execute the li3 console command and the cron.php script.