Documentation / LemmeSee Hub

Scripts and daemons

The hub can run your own Python scripts in two ways. A script task runs a script on a schedule and waits for it to finish. A daemon task starts a script that keeps running, such as a bot or a monitor, and restarts it when it crashes. Both run with the same Python the hub runs on.

To add one, open the Tasks page, choose + Add, and pick Script or Daemon. The fields are listed in Tasks.

Script tasks

A script task runs each time its schedule comes round (see Scheduling), or when you press Run now.

What your script doesWhat the hub does
Prints a line (stdout or stderr)Publishes it as a debug log entry on the private channel, under the task's source name. Empty lines are skipped.
Exits with code 0Records the run as ok.
Exits with any other codeRecords the run as error, with the message Script exited with code N, and publishes an error log entry from the source LemmeSee: Task 'Nightly backup' failed: ....
Can't be foundPublishes an error log entry Script not found: ... and records the run as error.

The script runs from the hub folder, so a relative path in your script (or in the task's path) is relative to the hub folder.

Daemon tasks

A daemon starts when the hub starts (or when you enable the task) and runs until it exits or you stop it. It runs from the folder its script is in.

What your daemon doesWhat the hub does
StartsStatus running. The hub logs [source] starting.
Exits with code 0Status stopped, and it is not restarted. The hub logs [source] exited cleanly (code 0). Exit with 0 when your daemon means to stop on its own.
Exits with any other code, or crashesStatus restarting. The hub logs a warning with the exit code, waits restart_delay seconds, and starts it again. Each restart adds to the restart count on the task card.
Is stopped from the Tasks page, or the hub shuts downThe hub ends the process. Status stopped.

The hub's own messages about daemons come from the source LemmeSee.

Output

By default a daemon's printed output is thrown away. Turn on capture_output ("Forward stdout/stderr to debug log") and each printed line becomes a private debug log entry under the daemon's source. With capture on:

  • A line repeated five or more times within a minute is collapsed: you see (suppressing repeated: ...) once, then (last repeated N×: ...) when a different line arrives.
  • A few common startup banners from web frameworks, and [mqtt] connected to ... lines, are dropped.

Python output is unbuffered and UTF-8 for daemons, so lines show up as soon as they're printed.

Sending real log entries

Printed output always arrives at debug level. To send entries at other levels, or public ones, use the log_client.py helper from your script. The hub download includes it in its helpers folder; copy it next to your script:

import log_client

log_client.SOURCE = "backup"
log_client.info("Backup started")
log_client.error("Backup failed: target not reachable", public=True)

For a daemon, the log_client.SOURCE = "backup" line also gives the hub its source name when you leave source_name blank. See Logs for the full helper, and Pings for pings.

Give your sources friendly labels in log_sources in config.json. See Configuration.

Environment

Scripts and daemons inherit the hub's environment variables, plus:

VariableSet forValue
LEMMESEE_SOURCEScripts and daemonsThe task's source name.
PYTHONIOENCODINGDaemonsutf-8
PYTHONUNBUFFEREDDaemons1

The helper doesn't read LEMMESEE_SOURCE by itself. To use it:

import os
import log_client

log_client.SOURCE = os.environ.get("LEMMESEE_SOURCE", "backup")

The Tasks page and the Dashboard show each daemon's status, uptime and restart count.