Run a long time-consuming task through commandline?

I have a multiselect field like:

Tags: Item A, Item B ...

This field needs to be changed to a structured field to add more context like so:

Note: Hello World
Tags: Item A
Note: Hi
Tags: Item B

Basically I thought to grab the collection of pages this needs to run on and use the addToStructure function as per How to Programatically Add Entries to Structure Field - #3 by pedroborges

The problem is that this takes about 2-3s to execute per page, and there are thousands of pages which results in a very long execution time. Somewhat sloppily, I had my migration bound to a route in a plugin function and was trying to run it by curl’ing the endpoint with no timeout. I increased my PHP execution time to something massive like 90 minutes, but things still aren’t working properly (probably nginx issues in between).

In Avoid Php script timeout @lukasbestle mentions running scripts in the commandline instead and was wondering how to best go about doing something like this?

As @bnomei was part of that conversation and is the author of Kirby Janitor, have I answered my own question?

To run a Kirby script from the command line, it’s easiest to copy the index.php file from the root of your Kirby site to another file (like script.php). Then you can replace the render() line with your code:


require __DIR__ . '/kirby/bootstrap.php';

$kirby = new Kirby();

// your custom code

You can run this script like this:

php script.php

Make sure to delete the script again when you are done.

1 Like

Thank you for the response, I ended up doing this through a custom janitor job, using the backup script as an example to provide some more feedback/visibility of the progress via ‘climate’.