2014-10-17 10:16:50 +02:00
|
|
|
title: Command Line Interface
|
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-10-17 10:16:50 +02:00
|
|
|
introduction: Automate SilverStripe, run Cron Jobs or sync with other platforms through the Command Line Interface.
|
2014-09-21 02:07:58 +02:00
|
|
|
|
2019-02-27 02:50:49 +01:00
|
|
|
SilverStripe can call [Controllers](../controllers) through a command line interface (CLI) just as easily as through a
|
|
|
|
web browser. This functionality can be used to automate tasks with cron jobs, run unit tests, or anything else that
|
2014-10-17 10:16:50 +02:00
|
|
|
needs to interface over the command line.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2017-10-02 13:30:24 +02:00
|
|
|
The main entry point for any command line execution is `cli-script.php` in the framework module.
|
|
|
|
For example, to run a database rebuild from the command line, use this command:
|
2017-08-03 02:51:32 +02:00
|
|
|
|
2017-10-27 04:38:27 +02:00
|
|
|
```bash
|
|
|
|
cd your-webroot/
|
|
|
|
php vendor/silverstripe/framework/cli-script.php dev/build
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
<div class="notice">
|
2019-02-27 02:50:49 +01:00
|
|
|
Your command line php version is likely to use a different configuration as your webserver (run `php -i` to find out
|
2014-09-27 07:53:21 +02:00
|
|
|
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-10-17 10:16:50 +02:00
|
|
|
## Sake - SilverStripe Make
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2019-02-27 02:50:49 +01:00
|
|
|
Sake is a simple wrapper around `cli-script.php`. It also tries to detect which `php` executable to use if more than one
|
2017-10-25 12:48:50 +02:00
|
|
|
are available. It is accessible via `vendor/bin/sake`.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
<div class="info" markdown='1'>
|
2019-02-27 02:50:49 +01:00
|
|
|
If you are using a Debian server: Check you have the php-cli package installed for sake to work. If you get an error
|
2014-09-27 07:53:21 +02:00
|
|
|
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
|
|
|
|
|
2017-10-25 12:48:50 +02:00
|
|
|
`sake` can be invoked using `./vendor/bin/sake`. For easier access, copy the `sake` file into `/usr/bin/sake`.
|
2017-10-27 04:38:27 +02:00
|
|
|
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
2017-10-27 04:38:27 +02:00
|
|
|
cd your-webroot/
|
|
|
|
sudo ./vendor/bin/sake installsake
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
2017-10-27 04:38:27 +02:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
<div class="warning">
|
2014-10-17 10:16:50 +02:00
|
|
|
This currently only works on UNIX like systems, not on Windows.
|
2014-09-27 07:53:21 +02:00
|
|
|
</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
|
|
|
|
2019-02-27 02:50:49 +01: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 the site in a web browser this is easy to work out, but when executing scripts on the
|
2017-04-13 03:33:29 +02:00
|
|
|
command line, it has no way of knowing.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2017-04-13 03:33:29 +02:00
|
|
|
You can use the `SS_BASE_URL` environment variable to specify this.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2017-01-30 16:35:43 +01:00
|
|
|
```
|
2017-04-13 03:33:29 +02:00
|
|
|
SS_BASE_URL="http://localhost/base-url"
|
2017-01-30 16:35:43 +01:00
|
|
|
```
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
### Usage
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2017-10-25 12:48:50 +02:00
|
|
|
`sake` can run any controller by passing the relative URL to that controller.
|
2014-10-17 10:16:50 +02:00
|
|
|
|
2017-08-03 02:51:32 +02:00
|
|
|
|
|
|
|
```bash
|
2017-10-27 04:38:27 +02:00
|
|
|
sake /
|
|
|
|
# returns the homepage
|
2017-08-03 02:51:32 +02:00
|
|
|
|
2017-10-27 04:38:27 +02:00
|
|
|
sake dev/
|
|
|
|
# shows a list of development operations
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
2014-10-17 10:16:50 +02:00
|
|
|
|
2017-10-25 12:48:50 +02:00
|
|
|
`sake` is particularly useful for running build tasks.
|
2017-08-03 02:51:32 +02:00
|
|
|
|
2017-10-27 04:38:27 +02:00
|
|
|
```bash
|
|
|
|
sake dev/build "flush=1"
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
2014-10-17 10:16:50 +02:00
|
|
|
|
2019-02-27 02:50:49 +01:00
|
|
|
<div class="alert" markdown="1">
|
|
|
|
You have to run "sake" with the same system user that runs your web server,
|
|
|
|
otherwise "flush" won't be able to clean the cache properly.
|
|
|
|
</div>
|
|
|
|
|
2014-10-17 10:16:50 +02:00
|
|
|
It can also be handy if you have a long running script..
|
2017-08-03 02:51:32 +02:00
|
|
|
|
2017-10-27 04:38:27 +02:00
|
|
|
```bash
|
|
|
|
sake dev/tasks/MyReallyLongTask
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
### Running processes
|
|
|
|
|
2014-10-17 10:16:50 +02:00
|
|
|
`sake` can be used to make daemon processes for your application.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2019-02-27 02:50:49 +01:00
|
|
|
Make a task or controller class that runs a loop. To avoid memory leaks, you should make the PHP process exit when it
|
2014-10-17 10:16:50 +02:00
|
|
|
hits some reasonable memory limit. Sake will automatically restart your process whenever it exits.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2019-02-27 02:50:49 +01:00
|
|
|
Include some appropriate `sleep()`s so that your process doesn't hog the system. The best thing to do is to have a short
|
2014-10-17 10:16:50 +02:00
|
|
|
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:
|
|
|
|
|
2017-08-03 02:51:32 +02:00
|
|
|
|
|
|
|
```php
|
2017-10-27 04:38:27 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-08-05 00:45:24 +02:00
|
|
|
|
2019-02-27 02:50:49 +01:00
|
|
|
class MyProcess extends Controller
|
2017-10-27 04:38:27 +02:00
|
|
|
{
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2017-10-27 04:38:27 +02:00
|
|
|
private static $allowed_actions = [
|
|
|
|
'index'
|
|
|
|
];
|
2013-06-08 05:14:53 +02:00
|
|
|
|
2017-10-27 04:38:27 +02:00
|
|
|
function index() {
|
|
|
|
set_time_limit(0);
|
2013-06-08 05:14:53 +02:00
|
|
|
|
2017-10-27 04:38:27 +02:00
|
|
|
while(memory_get_usage() < 32*1024*1024) {
|
|
|
|
if($this->somethingToDo()) {
|
|
|
|
$this->doSomething();
|
|
|
|
sleep(1)
|
|
|
|
} else {
|
|
|
|
sleep(300);
|
2017-08-07 05:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-27 04:38:27 +02:00
|
|
|
}
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-10-17 10:16:50 +02:00
|
|
|
Then the process can be managed through `sake`
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2017-08-03 02:51:32 +02:00
|
|
|
```bash
|
2017-10-27 04:38:27 +02:00
|
|
|
sake -start MyProcess
|
|
|
|
sake -stop MyProcess
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
2014-10-17 10:16:50 +02:00
|
|
|
|
2014-09-27 07:53:21 +02:00
|
|
|
<div class="notice">
|
2014-10-17 10:16:50 +02:00
|
|
|
`sake` stores `pid` and log files in the site root directory.
|
2014-09-27 07:53:21 +02:00
|
|
|
</div>
|
2011-02-07 07:48:44 +01:00
|
|
|
|
2014-10-17 10:16:50 +02:00
|
|
|
## Arguments
|
2013-04-15 00:47:59 +02:00
|
|
|
|
2014-10-17 10:16:50 +02:00
|
|
|
Parameters can be added to the command. All parameters will be available in `$_GET` array on the server.
|
2013-04-15 00:47:59 +02:00
|
|
|
|
2017-08-03 02:51:32 +02:00
|
|
|
```bash
|
2017-10-27 04:38:27 +02:00
|
|
|
cd your-webroot/
|
|
|
|
php vendor/silverstripe/framework/cli-script.php myurl myparam=1 myotherparam=2
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
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
|
|
|
|
2017-08-03 02:51:32 +02:00
|
|
|
```bash
|
2017-10-27 04:38:27 +02:00
|
|
|
vendor/bin/sake myurl "myparam=1&myotherparam=2"
|
2017-08-03 02:51:32 +02:00
|
|
|
```
|
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-10-17 10:16:50 +02:00
|
|
|
On a UNIX machine, you can typically run a scheduled task with a [cron job](http://en.wikipedia.org/wiki/Cron). Run
|
2019-02-27 02:50:49 +01:00
|
|
|
`BuildTask` in SilverStripe as a cron job using `sake`.
|
2014-10-17 10:16:50 +02:00
|
|
|
|
|
|
|
The following will run `MyTask` every minute.
|
2014-09-21 02:07:58 +02:00
|
|
|
|
2017-08-03 02:51:32 +02:00
|
|
|
```bash
|
2017-10-27 04:38:27 +02:00
|
|
|
* * * * * /your/site/folder/vendor/bin/sake dev/tasks/MyTask
|
2017-10-02 13:30:24 +02:00
|
|
|
```
|