2014-09-27 07:53:21 +02:00
|
|
|
summary: Automate SilverStripe, run Cron Jobs or sync with other platforms through the Command Line Interface.
|
2014-09-21 02:07:58 +02:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
# Command Line
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
SilverStripe can call [controllers](../controllers) through a command line interface (CLI) just as easily as through a
|
|
|
|
web browser. This can be used to automate tasks with cron jobs, run unit tests, or anything else that needs to interface
|
|
|
|
over the command line.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
The main entry point for any command line execution is `cli-script.php`. For example, to run a database rebuild
|
|
|
|
from the command line, use this command:
|
|
|
|
|
|
|
|
:::bash
|
2011-02-07 07:48:44 +01:00
|
|
|
cd your-webroot/
|
2012-03-24 22:16:59 +01:00
|
|
|
php framework/cli-script.php dev/build
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
<div class="notice">
|
|
|
|
Your command line php version is likely to use a different configuration as your webserver (run `php -i` to find out
|
|
|
|
more). This can be a good thing, your CLI can be configured to use higher memory limits than you would want your website
|
|
|
|
to have.
|
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
## Sake: SilverStripe Make
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
Sake is a simple wrapper around `cli-script.php`. It also tries to detect which `php` executable to use if more than one
|
|
|
|
are available.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
<div class="info" markdown='1'>
|
|
|
|
If you are using a debian server: Check you have the php-cli package installed for sake to work. If you get an error
|
|
|
|
when running the command php -v, then you may not have php-cli installed so sake won't work.
|
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
### Installation
|
|
|
|
|
|
|
|
You can copy the `sake` file into `/usr/bin/sake` for easier access (this is optional):
|
|
|
|
|
|
|
|
cd your-webroot/
|
2012-03-24 22:16:59 +01:00
|
|
|
sudo ./framework/sake installsake
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
<div class="warning">
|
|
|
|
This currently only works on UNIX-like systems, not on Windows.
|
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
### Configuration
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
Sometimes SilverStripe needs to know the URL of your site, for example, when sending an email or generating static
|
|
|
|
files. When you're visiting your site in a web browser this is easy to work out, but if you're executing scripts on the
|
|
|
|
command line, it has no way of knowing.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
To work this out, you should add lines of this form to your [_ss_environment.php](/getting_started/environment_management)
|
|
|
|
file.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
global $_FILE_TO_URL_MAPPING;
|
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
$_FILE_TO_URL_MAPPING['/Users/sminnee/Sites'] = 'http://localhost';
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
The above statement tells SilverStripe that anything executed under the `/Users/sminnee/Sites` directly will have the
|
|
|
|
base URL `http://localhost`.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
You can add multiple file to url mapping definitions. The most specific mapping will be used. For example:
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
global $_FILE_TO_URL_MAPPING;
|
2014-09-27 07:53:21 +02:00
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
$_FILE_TO_URL_MAPPING['/Users/sminnee/Sites'] = 'http://localhost';
|
|
|
|
$_FILE_TO_URL_MAPPING['/Users/sminnee/Sites/mysite'] = 'http://mysite.localhost';
|
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
### Usage
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
Sake is particularly useful for running build tasks
|
|
|
|
|
|
|
|
:::bash
|
2011-02-07 07:48:44 +01:00
|
|
|
cd /your/site/folder
|
2012-06-20 05:00:31 +02:00
|
|
|
sake dev/build "flush=1"
|
2011-02-07 07:48:44 +01:00
|
|
|
sake dev/tests/all
|
|
|
|
|
|
|
|
It can also be handy if you have a long running script.
|
2014-09-27 07:53:21 +02:00
|
|
|
|
|
|
|
:::bash
|
2011-02-07 07:48:44 +01:00
|
|
|
cd /your/site/folder
|
2012-06-20 05:00:31 +02:00
|
|
|
sake dev/tasks/MyReallyLongTask
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
### Running processes
|
|
|
|
|
|
|
|
You can use sake to make daemon processes for your application.
|
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
Step 1: Make a task or controller class that runs a loop. To avoid memory leaks, you should make the PHP process exit
|
|
|
|
when it hits some reasonable memory limit. Sake will automatically restart your process whenever it exits.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
Step 2: Include some appropriate sleep()s so that your process doesn't hog the system. The best thing to do is to have
|
|
|
|
a short sleep when the process is in the middle of doing things, and a long sleep when doesn't have anything to do.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
This code provides a good template:
|
|
|
|
|
|
|
|
:::php
|
2014-09-27 07:53:21 +02:00
|
|
|
<?php
|
|
|
|
|
2011-02-07 07:48:44 +01:00
|
|
|
class MyProcess extends Controller {
|
|
|
|
|
2013-06-08 05:14:53 +02:00
|
|
|
private static $allowed_actions = array(
|
|
|
|
'index'
|
|
|
|
);
|
|
|
|
|
|
|
|
function index() {
|
|
|
|
set_time_limit(0);
|
|
|
|
|
|
|
|
while(memory_get_usage() < 32*1024*1024) {
|
|
|
|
if($this->somethingToDo()) {
|
|
|
|
$this->doSomething();
|
|
|
|
sleep(1)
|
|
|
|
} else {
|
|
|
|
sleep(300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
Step 3: Install the "daemon" command-line tool on your server.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
Step 4: Use sake to start and stop your process
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
:::bash
|
2011-02-07 07:48:44 +01:00
|
|
|
sake -start MyProcess
|
|
|
|
sake -stop MyProcess
|
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
<div class="notice">
|
|
|
|
Sake stores Pid and log files in the site root directory.
|
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
## GET parameters as arguments
|
2013-04-15 00:47:59 +02:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
You can add parameters to the command by using normal form encoding. All parameters will be available in `$_GET` within
|
|
|
|
SilverStripe. Using the `cli-script.php` directly:
|
2013-04-15 00:47:59 +02:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
:::bash
|
|
|
|
cd your-webroot/
|
|
|
|
php framework/cli-script.php myurl myparam=1 myotherparam=2
|
2013-04-15 00:47:59 +02:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
Or if you're using `sake`
|
2013-04-15 00:47:59 +02:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
:::bash
|
|
|
|
sake myurl "myparam=1&myotherparam=2"
|
2013-06-08 05:14:53 +02:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
## Running Regular Tasks With Cron
|
2013-04-15 00:47:59 +02:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
On a UNIX machine, you can typically run a scheduled task with a [cron job](http://en.wikipedia.org/wiki/Cron). You can
|
|
|
|
execute any `BuildTask` in SilverStripe as a cron job using `Sake`.
|
2014-09-21 02:07:58 +02:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
:::bash
|
|
|
|
* * * * * /your/site/folder/sake dev/tasks/MyTask
|