Skip to content

Docker: Schedule Jobs

In the other tutorials, we've looked at polling a HTTP server and building a Docker container, reading from a serial port, and publishing to the history database.

All these scripts were using an interval of a few seconds at most. Sometimes that's way too often. For example: Brewfather requires a minimum interval of 15 minutes.

Simply adding a sleep() call for 15 minutes is a very unreliable solution. If your service stops or crashes, it will cancel the interval, leading to very short or very long gaps.

A more practical solution is to use the schedule package.

Source code

On your Pi, create a new directory scheduledscript. in it, create two files:

Code example for publishing data to the Brewblox eventbus on a fixed schedule

- paho-mqtt
- schedule

import json
from random import random
from time import sleep

import schedule
from paho.mqtt import client as mqtt

# is the default IP address for the host running the Docker container
# Change this value if Brewblox is installed on a different computer
HOST = ''

# 1883 is the default port for MQTT, but this can be changed in brewblox env settings.
PORT = 80

# This is a constant value. You never need to change it.
HISTORY_TOPIC = 'brewcast/history'

# The history service is subscribed to all topics starting with 'brewcast/history'
# We can make our topic more specific to help debugging
TOPIC = HISTORY_TOPIC + '/scheduledscript'

# Create the MQTT client
client = mqtt.Client()

def publish():

        client.connect_async(host=HOST, port=PORT)

        value = 20 + ((random() - 0.5) * 10)
        message = {
            'key': 'scheduledscript',
            'data': {'value[degC]': value}

        client.publish(TOPIC, json.dumps(message))
        print(f'sent {message}')


# For more examples on how to schedule tasks, see:

while True:


FROM python:3.11-slim

COPY /app/

RUN pip3 install paho-mqtt schedule

CMD ["python3", "-u", "/app/"]


Your scheduledscript directory should look like this:

└── Dockerfile

To build the image, run:

docker build --tag scheduledscript scheduledscript/


To run the built image:

docker run --rm --tty scheduledscript

This is exactly the same as the command in the dockerized script tutorial.


It's often useful to listen in on what messages the eventbus actually received.

You can do so using Connect to mqtt://PI_ADDRESS:1883, and you can see all published events.