Systemd Run - Systemd Transient Units

Some Context

I have relied on systemd to orchestrate daemons on my computer running Arch Linux for a while now. Recently, I was trying to get VPN setup on my machine using systemd. The VPN came with a bunch of OpenVPN config files (*.ovpn), each file had a set of 'remotes' corresponding to a single country. I could set up a systemd service unit for each country / openvpn config, but that would be very tedious and the maintenance of these service units would be a nightmare.

Solution - systemd-run

While trying to find a reasonable solution to this problem, I came across "systemd-run". The idea is simple, you can spawn ephemeral systemd service units that are automatically removed when stopped. This was perfect for my use case.

So, in the directory containing all the VPN configs, I created a shell script ( that looked something like this:


ls *.ovpn | fzf | xargs sudo systemd-run --unit=devtechnica-vpn --property=WorkingDirectory=/home/devtechnica/.vpnconf/ /usr/bin/openvpn --config

Now when I run the script and select an OpenVPN config file from the FZF prompt, I get the following output:

Running as unit: devtechnica-vpn.service; invocation ID: 1f049639793f4171bbdbae8d4829b8bc

I can now interact with the "devtechnica-vpn.service" as though it were a normal service. I can run sudo systemctl status devtechnica-vpn to check the status of the service, I can restart the service using sudo systemctl restart devtechnica-vpn.

Finally, if I want to kill off the service, I can just run sudo systemctl stop devtechnica-vpn. The service completely disappears.

I did however run into a small issue, where if the service fails, I was unable to stop it or kill it. I was able to remove the service unit using sudo systemctl reset-failed devtechnica-vpn.

So, there it is. systemd-run - create & orchestrate systemd transient units. There are many more uses for systemd-run that I haven't covered here. You can take a look at the systemd-run man pages to learn more about systemd-run.