Skip to Content

Scheduled tasks

Run a command on a cron schedule, on its own or inside one of your services' image and variables.

Scheduled tasks is an add-on that holds your cron jobs. Each task has a name, a cron schedule and a command, and runs either Standalone or inside a connected service, with that service's image and environment variables.

Add it

On the canvas, click + Service → Add-on → Scheduled tasks (Cron jobs as their own node — connect them to a service afterwards). Click the Scheduled tasks card, then Deploy.

Scheduled tasks: Ready to deploy, with Remove from canvas and Deploy
Scheduled tasks: Ready to deploy, with Remove from canvas and Deploy

Add a task

When it is Running, click the card and open the Scheduled tasks tab. Click Add task.

The Scheduled tasks tab, empty, with Add task
The Scheduled tasks tab, empty, with Add task

Fill in:

  • Name, for example hello.
  • Schedule, in cron syntax (Minute hour day month weekday). The editor starts with 0 2 * * * (every day at 02:00); this guide uses */30 * * * *.
  • Runs in: Standalone ("Runs on its own — no service needed") or one of your services, here nginx-9agm, which runs it with that service's image and env vars.
  • Command, for example /bin/sh -c 'echo hello from nginx; nginx -v'.

Add scheduled task adds another one in the same editor. Click Stage.

The task editor: Name, Schedule, Runs in, Command, Add scheduled task and Stage
The task editor: Name, Schedule, Runs in, Command, Add scheduled task and Stage

Deploy

Click Commit 2 changes. Running the task in a service also connects the two (nginx ⇄ scheduled-tasks off → on). Click Deploy 2 changes.

The task now shows its schedule, where it runs and when it fires next, for example hello · */30 * * * * in nginx-9agm · next run in 21m · never ran yet.

The task list: hello, */30 * * * * in nginx-9agm, next run, Run now
The task list: hello, */30 * * * * in nginx-9agm, next run, Run now

Run it now and read the output

Click Run now ("Triggered hello"). The Deployment tab lists the runs — scheduled, manual and after-deploy — with their result, for example Run succeeded for hello · manual. Refresh reloads the list.

Runs: Run succeeded for hello, manual
Runs: Run succeeded for hello, manual

The task's menu (next to Run now) has View logs, Edit, Pause and Remove. View logs shows the task's output:

The hello task's logs: hello from nginx and nginx version
The hello task's logs: hello from nginx and nginx version

Pause, edit or remove a task

Each of these is a canvas change: it is staged until you click Commit and Deploy.

  • Pause stops the task from firing. Once deployed, the task shows paused instead of its next run, and the menu offers Resume in place of Pause. Ours, paused at 23:17, skipped its 23:30 run. Run now still works on a paused task. Resume, committed and deployed, brings the schedule back ("next run in 25m").
  • Edit reopens the task editor with every task, to change a name, schedule, where it runs or its command. Click Stage when you're done.
  • Remove takes the task out. Removing the last task that runs in a service also disconnects that service: the review lists Scheduled tasks with 0 tasks and nginx ⇄ scheduled-tasks on → off.
The task list: hello, */30 * * * * in nginx-9agm, paused
The task list: hello, */30 * * * * in nginx-9agm, paused
The menu of a paused task: View logs, Edit, Resume and Remove
The menu of a paused task: View logs, Edit, Resume and Remove
2 changes to apply: Scheduled tasks 0 tasks, and nginx ⇄ scheduled-tasks on → off
2 changes to apply: Scheduled tasks 0 tasks, and nginx ⇄ scheduled-tasks on → off

How runs behave

  • Schedules are in UTC. There is no time zone setting. A task set to 53 7 * * * ran at 07:53 UTC.
  • Each run is a new container, with the image and variables of the service it runs in (or of its own for Standalone). Nothing it writes to its own disk is kept for the next run: keep state in a database or on a volume.
  • The exit code decides the result. 0 is a success. A task with sh -c 'echo about to fail; exit 3' was listed as Run failed.
  • Runs of one task never overlap. While a run is still going, the next one doesn't start. A task on * * * * * that sleeps 150 seconds started at 07:50:00. The 07:51 and 07:52 runs didn't start; the next run started at 07:52:33, right after the first one ended.
  • Each run gets the size of one instance of the service.

The run list

Click a run to see its logs. Each run's menu has:

  • View logs.
  • Run again: start the same task now.
  • Cancel, while the run is pending or running.
  • Remove, once it has finished: deletes the run from the list.

Cron syntax

The five fields are minute, hour, day of the month, month and day of the week.

ScheduleRuns
*/15 * * * *Every 15 minutes
0 * * * *At the start of every hour
0 2 * * *Every day at 02:00 UTC
30 6 * * 1-5Monday to Friday at 06:30 UTC
0 0 1 * *On the first of every month at midnight UTC

The CLI also takes @hourly and @daily.

From the CLI and the API

stackblaze cron works on a service's tasks:

CommandWhat it does
stackblaze cron listThe tasks with their schedule, command and last run
stackblaze cron set report --schedule "0 3 * * *" --command "node report.js"Add a task or change one, in one release
stackblaze cron run reportRun it now
stackblaze cron logs reportThe logs of its runs, while their containers still exist
stackblaze cron delete reportRemove it

--command doesn't go through a shell: use sh -c '…' for pipes and ;. --image runs another image instead of the service's.

To start a run from your own code, call POST /api/apps/{project}/{environment}/{service}/cronjobs/{task}/trigger with an API token. {service} is the service's resource name, such as crondemo-txzo. The answer names the run:

{ "status": "ok", "job": "crondemo-txzo-kuberoapp-tzcheck-mmwvu2tjn", "cronjob": "crondemo-txzo-kuberoapp-tzcheck" }

On this page