Stage 3 · Build
systemd Service Management & Resource Control
Transient Units
systemd-run, scopes, timers, user services, and isolating ad hoc administrative commands.
systemd-run
systemd-run creates transient (temporary) units on the fly. It is useful for running commands under systemd control without writing unit files. Transient units are automatically cleaned up when they finish.
# Run a command as a transient service
sudo systemd-run --unit=cleanup --scope /usr/bin/cleanup.sh
# Run with resource limits
sudo systemd-run --unit=limited --scope \
--property=MemoryMax=512M \
--property=CPUQuota=50% \
/opt/myapp/bin/heavy-task
# Run in background
sudo systemd-run --unit=background-task --scope --no-block \
/opt/myapp/bin/long-task
# Run as specific user
sudo systemd-run --unit=user-task --scope \
--uid=www-data --gid=www-data \
/usr/bin/check-permissions
# List transient units
systemctl list-units --type=service --state=running | grep cleanup
# Check status
systemctl status cleanup.service
# Stop a transient unit
sudo systemctl stop cleanup.servicesystemd-run creates temporary units without writing files. The --scope flag creates a scope (process group), without it creates a service.
Transient Scopes
# Create a scope for existing processes
sudo systemd-run --scope --unit=mygroup.slice \
--property=MemoryMax=2G \
--property=CPUQuota=200% \
bash -c "echo $$ && sleep 3600"
# List scopes
systemctl list-units --type=scope
# Check scope properties
systemctl show mygroup.slice -p MemoryMax
# MemoryMax=2147483648
# Kill a scope
sudo systemctl kill mygroup.slice
# Remove a scope
sudo systemctl stop mygroup.slice
sudo systemctl reset-failed mygroup.slice
# Scopes for container runtimes
# Docker and containerd use scopes to group container processes
systemctl list-units --type=scope | grep docker
# docker-12345abc.scopeScopes are systemd units that manage external processes. Unlike services, scopes do not start/stop processes — they group them for resource control.
Transient Timers
# Run a command periodically
sudo systemd-run --unit=cleanup --scope --on-active=1h \
/usr/bin/cleanup.sh
# Run at specific time
sudo systemd-run --unit=report --scope --on-calendar="Mon *-*-* 09:00:00" \
/usr/bin/generate-report.sh
# Run with persistent timer
sudo systemd-run --unit=backup --scope \
--on-calendar=daily \
--property=Persistent=true \
/usr/bin/backup.sh
# List transient timers
systemctl list-timers --all | grep cleanup
# Check timer status
systemctl status cleanup.timerTransient timers combine systemd-run with timer functionality. They are useful for one-off scheduled tasks that don't need permanent unit files.
User Services
# Create user service directory
mkdir -p ~/.config/systemd/user/
# Create a user service
cat > ~/.config/systemd/user/myapp.service << 'EOF'
[Unit]
Description=My User Application
[Service]
ExecStart=/usr/bin/myapp
Restart=on-failure
[Install]
WantedBy=default.target
EOF
# Manage user services
systemctl --user daemon-reload
systemctl --user enable myapp.service
systemctl --user start myapp.service
systemctl --user status myapp.service
# List user services
systemctl --user list-units --type=service
# Enable lingering (start services at boot)
sudo loginctl enable-linger $(whoami)
# Check lingering status
loginctl show-user $(whoami) -p Linger
# Linger=yesUser services run under the user's account. Enable lingering to start them at boot. User services are ideal for personal applications and development tools.
Ad Hoc Commands
# One-shot command with resource limits
sudo systemd-run --scope --property=MemoryMax=1G \
python3 /opt/scripts/analyze.py
# Run a command and get its output
sudo systemd-run --unit=output-test --scope -- \
/bin/bash -c "echo 'hello' > /tmp/test.txt && cat /tmp/test.txt"
# Timer for a one-time future task
sudo systemd-run --unit=reminder --scope \
--on-calendar="2024-01-15 14:00:00" \
/bin/notify-send "Task reminder"
# Run with specific working directory
sudo systemd-run --unit=dir-test --scope \
--property=WorkingDirectory=/opt/myapp \
./run-tests.sh
# Group processes for resource control
sudo systemd-run --scope --unit=batch-group.slice \
--property=CPUQuota=100% \
--property=MemoryMax=2G \
bash -c "for i in {1..10}; do ./task.sh & done; wait"systemd-run is the fastest way to run commands under systemd control. Use it for ad hoc tasks that need resource limits, logging, or process management.
Transient vs Static Units
| Feature | Transient Units | Static Units |
|---|---|---|
| Persistence | Lost on stop/reboot | Persistent across reboots |
| Configuration | Command-line properties | Unit files on disk |
| Use case | Ad hoc, temporary | Production services |
| Management | systemd-run | systemctl + unit files |
| Version control | Not tracked | Can be in git |
systemd-run is excellent for testing resource limits, running commands with logging, and debugging service behavior without creating permanent unit files.
Transient units are removed when they stop. For production services, create permanent unit files in /etc/systemd/system/.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.