Overview
systemd-run executes commands under systemd's control, making them operate as independent systemd units. This allows you to leverage systemd's powerful features for process group management, resource limitations, and integrated logging.
Key Features
- Creation of transient scope or service units
- Setting resource limits and priorities for programs
- Background execution even after shell session termination (for service units)
- Integrated logging via the systemd journal
- Executing commands within containers and virtual machines
Key Options
systemd-run provides various options to control the execution environment of the program and the properties of the systemd unit.
Unit Type and Name
Execution Control and Environment
Generated command:
Try combining the commands.
Description:
`systemd-run` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Various usage examples of the systemd-run command.
Execute Command with Default Scope Unit
systemd-run --scope echo "Hello from systemd-run"
Executes a simple command as a transient scope unit. This command will run until the systemd-run process terminates.
Execute Command as a Background Service Unit
systemd-run --service --unit=my-long-task.service sleep 300
Creates a background service that continues to run even after the shell session ends. The service name is specified with `--unit`.
Set CPU Priority and Memory Limit
systemd-run --nice=10 --property=MemoryLimit=500M stress --cpu 4
Lowers the CPU priority of the program to be executed (nice=10) and limits its memory usage to 500MB.
Execute Command in a Specific Working Directory
systemd-run --working-directory=/tmp bash -c 'pwd; touch testfile.txt'
Configures the command to be executed from the `/tmp` directory.
Execute Piped Commands via Shell
systemd-run --shell echo "Hello" | cat
To use shell features like pipes (|) or redirection (>), you must use the `--shell` option.
Execute Command as a User Unit
systemd-run --user --scope echo "User-specific task"
Creates and runs a unit in the current user's systemd instance. This does not affect the system as a whole.
Tips & Notes
Useful tips and points to consider when using systemd-run.
Scope vs. Service Units
Scope units (`--scope`) are valid only while the `systemd-run` command is running and are dependent on the parent process. Service units (`--service`) run independently in the background even after the `systemd-run` command terminates. Using `--service` is common for background tasks.
- `--scope`: Dependent on the parent process, terminates with `systemd-run`
- `--service`: Independent background execution, persists after `systemd-run` terminates
Checking and Managing Unit Status
The status of units created by systemd-run can be checked using the `systemctl` command. Service units can be stopped using `systemctl stop`, etc.
- Check status: `systemctl status <unit_name>`
- Stop service: `systemctl stop <unit_name>`
- View logs: `journalctl -u <unit_name>`
Utilizing Resource Limits
You can limit various resources such as CPU, memory, and I/O using the `--property` option. This is highly effective in improving system stability and preventing specific processes from consuming excessive resources.
Using `--shell` for Shell Features
If your command includes shell-specific features like pipes (`|`), redirection (`>`), or background execution (`&`), you must use the `--shell` option to ensure the command is interpreted by the shell. Otherwise, the command may not execute correctly.