Systemd-run Examples (with examples)

Systemd-run Examples (with examples)

Starting a transient service

sudo systemd-run command argument1 argument2 ...

Motivation: This command is useful when you want to run a command as a transient service. It allows you to start a command in its own service unit, which can be controlled by systemd.

Explanation: The sudo systemd-run command starts a transient service with the specified command and arguments. This service will be managed by systemd and can be controlled using systemd commands.

Example Output: The specified command will be executed as a transient service, and the output will be displayed in the console.

Starting a transient service under the service manager of the current user (no privileges)

systemd-run --user command argument1 argument2 ...

Motivation: This command is useful when you want to start a transient service under the service manager of the current user without requiring root privileges. It allows users to run commands as services without administrative access.

Explanation: The systemd-run --user command starts a transient service under the service manager of the current user. This allows the user to run commands as services without requiring root privileges.

Example Output: The specified command will be executed as a transient service under the user’s service manager, and the output will be displayed in the console.

Starting a transient service with a custom unit name and description

sudo systemd-run --unit=name --description=string command argument1 argument2 ...

Motivation: This command is useful when you want to start a transient service with a custom unit name and description. It allows you to provide meaningful names and descriptions for the transient services.

Explanation: The sudo systemd-run --unit=name --description=string command starts a transient service with the specified unit name and description. This allows you to provide meaningful names and descriptions for the transient services, making it easier to manage and identify them.

Example Output: The specified command will be executed as a transient service with the custom unit name and description, and the output will be displayed in the console.

Starting a transient service that does not get cleaned up after it terminates with a custom environment variable

sudo systemd-run --remain-after-exit --set-env=name=value command argument1 argument2 ...

Motivation: This command is useful when you want to start a transient service that does not get cleaned up after it terminates and set a custom environment variable. It allows you to run commands that require specific environment variables to be set and persist the service even after it finishes.

Explanation: The sudo systemd-run --remain-after-exit --set-env=name=value command starts a transient service that does not get cleaned up after it terminates. The --set-env option allows you to set a custom environment variable for the transient service, which can be useful for providing configuration or specific runtime settings.

Example Output: The specified command will be executed as a transient service with the custom environment variable set, and the output will be displayed in the console.

Starting a transient timer that periodically runs its transient service

sudo systemd-run --on-calendar=calendar_event command argument1 argument2 ...

Motivation: This command is useful when you want to start a transient timer that periodically runs its transient service. It allows you to schedule the execution of commands at specific intervals or on specific dates.

Explanation: The sudo systemd-run --on-calendar=calendar_event command starts a transient timer that periodically runs its transient service based on the specified calendar event. The calendar event follows a specific format, which can be found in the man systemd.time manual page.

Example Output: The specified command will be executed as a transient service periodically based on the specified calendar event, and the output will be displayed in the console.

Sharing the terminal with the program and keeping execution details after exit

systemd-run --remain-after-exit --pty command

Motivation: This command is useful when you want to share the terminal with the program being executed. It allows for interactive input/output with the program and keeps the execution details visible even after the program exits.

Explanation: The systemd-run --remain-after-exit --pty command starts a transient service in PTY (pseudo-terminal) mode. This allows for interactive input/output with the program, meaning it can be used for running programs that require user interaction. The --remain-after-exit option ensures that the execution details of the program are retained even after the program exits.

Example Output: The specified command will be executed as a transient service in PTY mode, allowing for interactive input/output. The execution details will be displayed in the console even after the program exits.

Setting properties of the process and waiting until it exits

systemd-run --property MemoryMax=memory_in_bytes --property CPUQuota=percentage_of_CPU_time% --wait command

Motivation: This command is useful when you want to set specific properties of the process being executed and wait until it exits. It allows for fine-grained control over resource allocation and enables synchronization with the completion of the process.

Explanation: The systemd-run --property MemoryMax=memory_in_bytes --property CPUQuota=percentage_of_CPU_time% --wait command starts a transient service with specific properties set for the process. The --property option allows you to set various properties such as memory limits (MemoryMax) and CPU quotas (CPUQuota). The --wait option ensures that the command waits until the process exits before returning.

Example Output: The specified command will be executed as a transient service with the specified process properties set. The command will wait until the process exits, and the output will be displayed in the console.

Using the program in a shell pipeline

command1 | systemd-run --pipe command2 | command3

Motivation: This command is useful when you want to use the output of a command in a shell pipeline as the input for a systemd-managed transient service. It allows for chaining multiple commands together, with the transient service being a part of the pipeline.

Explanation: The command1 | systemd-run --pipe command2 | command3 command sets up a shell pipeline where the output of command1 is piped (|) as the input to command2. The output of command2 is then piped as the input to command3. The systemd-run --pipe command creates a transient service that takes the input from the pipeline and produces output that can be further processed by subsequent commands.

Example Output: The output of command1 is piped as the input to command2, which is executed as a transient service. The output of command2 is then piped as the input to command3, and the final output is displayed in the console.

Related Posts

How to use the command "fish" (with examples)

How to use the command "fish" (with examples)

The “fish” command is an acronym for “The Friendly Interactive SHell”.

Read More
How to use the command "circo" (with examples)

How to use the command "circo" (with examples)

The “circo” command is a part of the “graphviz” software package and is used to render an image of a circular network graph from a graphviz file.

Read More
Generating Shell Completions for rustup and cargo (with examples)

Generating Shell Completions for rustup and cargo (with examples)

Motivation: When working with the Rust programming language, it is often helpful to have shell completions for the rustup and cargo commands.

Read More