I have two systemd scripts that I want to run on boot up.

  1. The first systemd script starts OpenVPN, which then executes a shell script that writes the IP of the connection to the file vpn.env.

  2. The second systemd script starts Transmission and should bind to the IP adress in vpn.env.

My problem seems to be that the execution of the 2nd systemd script is too "quick", and is completed before OpenVPN can start and write vpn.env.

Question: Is there some way to add a delay to the second script, perhaps a few seconds, and have it wait for the environment file to be written?

systemd OpenVPN script

Description=VPN Custom Launch Connection

ExecStart=/usr/sbin/openvpn --cd /etc/openvpn --config /etc/openvpn/vpn.conf


OpenVPN .sh script, executed when program starts

printenv > /etc/openvpn/vpn.env

systemd Transmission script

Description=Transmission BitTorrent Daemon Under VPN
After=network.target vpn.service

ExecStart=/usr/bin/transmission-daemon -f --log-error --config-dir /opt/transmission --bind-address-ipv4 $ifconfig_local --rpc-bind-address --no-portmap
ExecReload=/bin/kill -s HUP $MAINPID

  • 1
    You could use a timer with OnBootSec= directive (example) – don_crissti Dec 6 '15 at 18:21
  • @don_crissti Thanks, the link helped me. If you like, you can make an answer out of it. I tested the answer below but it had no bearing on my problem unfortunately. – PAN Dec 13 '15 at 21:57
  • 1
    Well, not really as it answers the question in your title not your actual problem. Anyways, after reading your question again I can see the reason for the answer below... See also this thread on arch forums maybe that helps and you can eventually manage to make them work as "start A only after B exits" – don_crissti Dec 13 '15 at 22:23
  • So OpenVPN writes that .env file as part of its "coming up process" then continues running in the background? – rogerdpack Jul 9 '19 at 16:53

Your problem is the Type=simple in the description of the VPN service. The Arch wiki clarifies the manual page, a little:

Type=simple (default): systemd considers the service to be started up immediately. The process must not fork. Do not use this type if other services need to be ordered on this service, unless it is socket activated.

You probably can make this work by changing the type:

Type=oneshot: this is useful for scripts that do a single job and then exit. You may want to set RemainAfterExit=yes as well so that systemd still considers the service as active after the process has exited.

| improve this answer | |

You could add a delay: https://stackoverflow.com/q/43001223/32453

Or possibly could do this in your systemd OpenVPN service file:

 ExecStartPost=/bin/bash -c 'do_bash_loop_that_waits_till_file_appears_here'

And another option: create a systemd .path unit file. Maybe have it fire whenever that vpn.env file is created or changed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.