3

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

[Unit]
Description=VPN Custom Launch Connection
After=network.target

[Service]
Type=simple
ExecStart=/usr/sbin/openvpn --cd /etc/openvpn --config /etc/openvpn/vpn.conf

[Install]
WantedBy=multi-user.target

OpenVPN .sh script, executed when program starts

printenv > /etc/openvpn/vpn.env

systemd Transmission script

[Unit]
Description=Transmission BitTorrent Daemon Under VPN
After=network.target vpn.service
Requires=vpn.service

[Service]
User=transmission
Type=notify
EnvironmentFile=/etc/openvpn/vpn.env
ExecStart=/usr/bin/transmission-daemon -f --log-error --config-dir /opt/transmission --bind-address-ipv4 $ifconfig_local --rpc-bind-address 0.0.0.0 --no-portmap
ExecReload=/bin/kill -s HUP $MAINPID

[Install]
WantedBy=multi-user.target
  • 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. – Winterflags 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
7

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.

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.