I have a systemd service that sets up my openvpn tunnel. I also have a bash script that forwards a port for said tunnel. I want to have the script run after openvpn starts and I have been able to do that with an ExecStartPost calling my port forwarding script. The only issue is that the systemd service is Type=forking, and it seems that ExecStartPost must also be forking, which it is not, so when I try to restart the systemd service, it hangs forever, presumably waiting for the port forward script to daemonize. Is it possible to somehow execute the port forward script from the systemd service some other way? Or would it be possible to, perhaps, have two services run in tandem (e.g. one is executed strictly after the other, and restarts happen in sync as well)? I only ever run the port forward script when I start the VPN tunnel, so I would like to have it run automatically. Here is the systemd service file in question:

Description=OpenVPN connection to %i

ExecStart=/usr/sbin/openvpn --daemon ovpn-%i --status /run/openvpn/%i.status 10 --cd /etc/openvpn --script-security 2 --config /etc/openvpn/%i.conf --writepid /run/openvpn/%i.pid
ExecReload=/bin/kill -HUP $MAINPID
DeviceAllow=/dev/null rw
DeviceAllow=/dev/net/tun rw

  • 1
    Recent Openvpn versions support the notify type of service. With this, Openvpn will notify systemd when the tunnel is up. You might want to use this instead of forking – Torin Apr 9 '19 at 16:06
  • @Torin Hmm, I have changed the type from forking to notify, and it starts up finek, but I am having trouble figuring out how to call the script when the service is done starting up. Is there any guide to systemd-notify that puts things in layman's terms? I have not been able to find anything like that, unfortunately. – B-plow Apr 9 '19 at 17:58
  • I don't have the documentation to hand, but I think you need to remove the Daemon option. Does the script run correctly if you add it as a ExecStartPost when using notify? – Torin Apr 9 '19 at 21:06
  • @Torin Adding ExecStartPost, while preserving the --daemon option seems to make everything run as I want it to. Port gets forwarded, the service is reported to be running and it no longer hangs on start/restart. Thank you very much! – B-plow Apr 10 '19 at 17:17
  • 1
    Good to hear! If you've found a solution to the problem you may want to post it as an answer and accept it for anyone else having a similar issue – Torin Apr 10 '19 at 17:31

Your Answer

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

Browse other questions tagged or ask your own question.