Leaskovski
Posts: 13
Joined: Tue May 12, 2015 9:29 am

Run a script before a service starts on bootup

Thu Jan 05, 2017 9:08 pm

On my PI i have OpenVPN and it is configured to run using a tun (bridge) interface. In order for this to work, I have to run a script that I have created before the OpenVPN service starts so that the tun interface can be setup and OpenVPN can then use it. If I start my box, and then run my script and start OpenVPN, OpenVPN works how I want it to.

So I want to automate this on my headless setup, so that when the PI boots and the OpenVPN service is started automatically (init.d) at boot time, the script is run and OpenVPN will be able to use the interfaces setup by the script.

Originally, I had edited the /etc/init.d/openvpn code to manually add in my bash script so that it would be run on service startup so it worked fine, however the problem with this is that any OpenVPN updates mean I have to go in a redo this work.

To get around this I though I could use an upstart conf file (/etc/init) that is configured to fire on service startup for OpenVPN, however I don't think the PI uses upstart but systemd-sysv instead.

How should I go about running this script? I also have a script to tear down the tun interface for when I stop OpenVPN, however maybe I don't actually need to do this, and just leave the tun device up and running through out the uptime of the PI.

Thanks

User avatar
DougieLawson
Posts: 35519
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Run a script before a service starts on bootup

Thu Jan 05, 2017 9:41 pm

What's in /etc/default/openvpn? Change

Code: Select all

#AUTOSTART="all"
to

Code: Select all

AUTOSTART="all"
and some magic will happen.
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Leaskovski
Posts: 13
Joined: Tue May 12, 2015 9:29 am

Re: Run a script before a service starts on bootup

Thu Jan 05, 2017 10:04 pm

Thats not what I meant.

I mean I have a bash script that I have to run before the openvpn service starts (which it does today on boot) to configure a networking device (tun0) which an OpenVPN config is using. Without it the device isn't setup and thus the openvpn service fails to start.

I am trying to figure where I run that script? I am wandering if I just add it to "/etc/network/interfaces" along side another script that opens up some routes that means traffic for OpenVPN is allowed.

Return to “Raspbian”