berth

Apps and software

Scheduled tasks (cron)

Run a PHP script, fetch a URL or run a shell command on a schedule, inside the subscription's limits, never twice at once, with the result and output of the last runs.

Updated 4 min readcron · scheduled tasks · crontab · php script · jobsView as Markdown

On this page
  1. Add a task
  2. Three kinds of task
  3. How tasks run
  4. When the package changes
  5. Results and output
  6. Tasks the panel manages itself
  7. Tips

Scheduled tasks run something automatically at fixed times: a CMS's background jobs every quarter of an hour, a nightly export, a cleanup once a week. In Berth a task belongs to a subscription, runs as its system user within its resource limits, and keeps a log of its last runs so you can see whether it worked and what it printed.

The package sets how many tasks a subscription may have (berth plan set … --max-cron).

Add a task

On the domain page, click Scheduled tasks, then Add task:

  1. Enter the schedule: five fields (minute, hour, day of month, month, day of week), for example */15 * * * * for every quarter of an hour, or one of @hourly, @daily, @weekly.
  2. Choose what runs: a PHP script, a URL or a command (below).
  3. Optionally add a description, so you know later why the task exists.
  4. Click Add.
ScheduleRuns
*/15 * * * *every 15 minutes
0 3 * * *every night at 03:00
30 6 * * 1Mondays at 06:30
0 0 1 * *the first day of every month at midnight
@hourly, @daily, @weeklyonce an hour, a day, a week

Three kinds of task

PHP script

A script in the webspace, for example httpdocs/cron.php, optionally with arguments. It runs with the PHP version, limits and environment of the domain you choose, exactly like the website does: the same php.ini values, the same environment variables and secrets. This is the right choice for almost every CMS or framework job.

berth cron add --sysuser acme --domain example.com --schedule "*/15 * * * *" \
  --php-script httpdocs/cron.php --arg=--quiet --description "Background jobs"

A PHP-script task can only be added for a domain that has PHP.

Fetch a URL

The panel requests an address over HTTP or HTTPS, the way a visitor would. This suits applications that expose a cron endpoint. Addresses on the server itself or in an internal network are only allowed for your own domains.

berth cron add --sysuser acme --schedule "@hourly" --url https://example.com/cron/run

Shell command

Any command in the shell, for example a backup script or php8.3 artisan schedule:run. Tick Load the environment of … to give the command the same variables and secrets as a website. Commands are available only when the package allows shell access or lets PHP start processes; otherwise the choice is not offered.

berth cron add --sysuser acme --schedule "0 3 * * *" \
  --command "/var/www/vhosts/example.com/bin/export.sh" --env-domain example.com

How tasks run

  • Tasks run as the subscription's system user and apply to all domains of the subscription.
  • They run inside the subscription's resource slice, so CPU, memory and process limits of the package apply, and a runaway task cannot slow down other customers.
  • A task never runs twice at the same time. If it is still running when its next turn comes, that turn is skipped.
  • With the write lock on, tasks cannot write in the document roots, except in the writable exceptions.

When the package changes

If a package loses shell access (and PHP may not start processes), the panel suspends the subscription's command tasks instead of deleting them, with the reason shown at the task. They are kept and switch back on by themselves when the package allows them again. The same happens to a PHP-script task whose domain no longer has PHP.

Results and output

Each task shows its last run: succeeded or failed, when, how long it took, and on failure why, such as the exit code, a signal, or a message like "HTTP 500". Open Output of the last runs to see what the task wrote, stdout and stderr together.

Berth keeps the last 10 runs per task: the last 16 KiB of output of the newest run, and the last 2 KiB of older ones. The beginning of a long output falls off. Output is not mailed; the log lives in the subscription's own folder and is removed together with the task.

berth cron list --sysuser acme
berth cron log --id 42                 # the last runs of one task
berth cron log --sysuser acme --runs 1  # the latest run of every task
berth cron remove --id 42

Through the API, the same log is at GET /api/v1/cron/{id}/log; see REST API.

Tasks the panel manages itself

Some applications get their scheduler from the panel, so you do not need to add a task:

  • Laravel: the scheduler (schedule:run every minute) and queue workers on the domain's Laravel page; see Node.js and Python apps.
  • Nextcloud: background jobs through the system cron, set up at install.
  • Matomo: archiving through cron every hour, set up at install.

For long-running work that should not wait for a schedule, such as a queue consumer, use a background worker instead; see Node.js and Python apps.

Tips

  • Prefer a PHP script over a URL for CMS jobs: it does not depend on DNS, the certificate, the page cache or the per-visitor limits.
  • Use absolute paths in commands. The working directory and PATH of a task are not those of an interactive shell.
  • Make a task safe to run again. If a run fails halfway, the next run should pick up cleanly.
  • Spread heavy nightly tasks over different minutes instead of starting them all at 0 0 * * *.