Course contentsShow
← Linux
Lesson 1327 of 1,81911. Automation and System SchedulingFree lesson

Dynamic Unit Generation

Programmatically create systemd unit files to handle highly variable job requirements.

In this lesson, you will learn how to programmatically generate systemd unit files on-the-fly to handle highly variable job requirements that cannot be predicted at design time.

The Problem: Beyond Static Configurations

Standard automation relies on pre-written unit files stored in /etc/systemd/system/. However, in scenarios like dynamic task queuing, multi-tenant background processing, or on-demand resource provisioning, you often don't know the exact parameters of a job—such as target hostnames, specific user credentials, or custom environmental variables—until the moment the job is triggered. Rather than creating thousands of static files, we use Dynamic Unit Generation.

Orchestrating Units Programmatically

Dynamic generation involves using a "controller" script (often a long-running service or a simple shell wrapper) that creates a unit file dynamically in /run/systemd/system/ and then instructs systemd to reload its configuration.

Because /run/ is a tmpfs (RAM-backed) filesystem, these units are ephemeral; they vanish on reboot, preventing configuration clutter.

Here is a simplified pattern for generating a job-specific service:

# The controller script logic
JOB_ID="cleanup-$(date +%s)"
UNIT_FILE="/run/systemd/system/${JOB_ID}.service"

cat <<EOF > "$UNIT_FILE"
[Service]
ExecStart=/usr/local/bin/cleanup_task.sh --target=/data/jobs/${JOB_ID}
User=automation_worker
EOF

# Reload and run the unit
systemctl daemon-reload
systemctl start "${JOB_ID}.service"

Why This Matters

By generating units programmatically, you move the complexity from the unit file itself into your orchestration logic. This allows you to inject variables directly into the service definition at runtime without needing complex templating engines inside the unit files themselves. Since systemd treats these as first-class citizens once they are reloaded, you retain all the benefits of standard monitoring, dependency management, and resource sandboxing, even for jobs that only exist for a few minutes.

Key Takeaway: Dynamic unit generation enables scalable, ephemeral automation by creating unique service definitions in /run/systemd/system/ that systemd treats exactly like permanent configuration.