Posted on
Questions and Answers

Use `systemd-run` to launch transient services from a script

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Blog Article: Mastering systemd-run for Transient Services in Bash Scripts

Introduction

In the world of Linux, managing services and processes in a clean, efficient manner is crucial for good system administration. systemd, which has become the de facto init system for many Linux distributions, offers powerful tools for service management. One such tool is systemd-run, which allows the creation of transient services directly from the command line or scripts. In this blog, we explore how systemd-run can be used effectively to launch transient services from a script.

Q1: What is systemd-run?

systemd-run is a command that lets you run a command or a service with a systemd scope or service unit. It’s designed to create and start a transient service or scope unit and can be used for temporary tasks that need to be executed within the scope of systemd.

Q2: How does a transient service differ from a regular service?

A transient service, as opposed to a persistent service, is not permanently defined in the system files of systemd. It exists dynamically for as long as it’s needed and disappears after it's stopped without leaving lingering unit files.

Q3: Why would you want to use systemd-run from a Bash script?

Using systemd-run from a Bash script can be very useful for launching background tasks that need specific system resources, limits, or dependencies managed by systemd. It allows your script to benefit from systemd's capabilities, such as automatic restarting, logging, and isolation.

Q4: Can you provide a simple example of using systemd-run?

Certainly! Suppose you want to run a simple Bash script in the background that performs periodic backups every 30 seconds. You could invoke it using systemd-run like so:

systemd-run --unit=my-backup-task --on-active=30s /path/to/backup-script.sh

This command would create a transient service unit called my-backup-task that starts the script /path/to/backup-script.sh and runs it every 30 seconds.

Further Examples and Explanation

To solidify understanding, let's consider a common usage scenario:

Scenario: Running a Python Script as a Transient Service

Imagine you have a Python script that needs to run as a service and perform tasks at regular intervals or under specific system conditions. Using systemd-run, you can easily set this up without creating a permanent service file.

systemd-run --unit=python-task --slice=myslice.slice --remain-after-exit /usr/bin/python3 /home/user/myscript.py

This command runs the Python script /home/user/myscript.py as a transient service in a system slice called myslice.slice. The --remain-after-exit option ensures that the service is considered active until it is explicitly stopped, even if the script execution completes.

Executable Script Demo: Creating a Transient Logging Service

Here’s a Bash script example that sets up a transient service to log system memory usage every 10 seconds.

#!/bin/bash
# Filename: launch_logging_service.sh

cat <<EOF > /tmp/log_memory.sh
#!/bin/bash
while true
do
    echo "Memory Usage at \$(date): \$(free -h | grep Mem)" >> /var/log/memory.log
    sleep 10
done
EOF

chmod +x /tmp/log_memory.sh
systemd-run --unit=log-memory-service /tmp/log_memory.sh

This script creates another script /tmp/log_memory.sh that logs the memory usage to /var/log/memory.log every 10 seconds and uses systemd-run to run it as a service.

Conclusion

Using systemd-run to launch transient services from a script offers a highly flexible and powerful way to manage temporary or dynamic tasks without cluttering your system with permanent service files. It leverages the robustness of systemd to enhance script functionality with minimal overhead, optimizing both performance and system resource utilization.

Whether you're managing background tasks, setting up timed operations, or controlling resource usage, systemd-run introduces a level of control and integration valuable for any Linux system administrator or power user.

Further Reading

Here are some further reading resources to delve deeper into systemd and transient services:

  • Understanding systemd: An introduction and overview of systemd, including its components and functionalities. Link to resource

  • systemd-run reference manual: The official manual for systemd-run, detailing various options and use cases. Link to resource

  • Creating Systemd Service Files: A guide on how to create and manage systemd service files for persistent services. Link to resource

  • Using systemd for Process Management: Detailed examples and explanations on managing processes with systemd tools. Link to resource

  • Advanced systemd Techniques: A deep dive into more advanced systemd features, including transient units and system slices. Link to resource