On my Raspbian machine, I want a successful OpenVPN connection to my VPN provider to initiate a Python script, that persistently should run in the background as long as the VPN connection is active. If/when the VPN connection goes offline, I would like to terminate the Python script.
My thinking is this: the
down flags in OpenVPN's configuration file will execute shell scripts that start and stop a
systemd service. That service
openvpn.service and cannot (should not) run without it.
What's complicating things is that I want to run the Python script inside a
Virtualenv which in bash is accessible by
alias coding="cd /home/p1/Coding/Python/Virtual/py279/bin; source activate; cd /home/p1/Coding/Python/Projects". The location of the Python file is in the latter folder.
However, it is my understanding that
systemd cannot run command lines in the way as the
alias coding above would.
So I guess a solution could be for
systemd to execute a
.service that executes a
.sh script that in turn executes the
.py Python script.
sudo systemctl start vpn_up.service→
coding; python my_script.py
sudo systemctl stop vpn_up.service
My assumption is that there are a few caveats and/or problems with this, especially regarding how to properly terminate the Python script.
- Is this feasible?
- What sections and service commands in the service files are needed for this to work?
- Are there any special considerations that must be made for this overall arrangement, e.g. proper clean-up after terminating the Python script, what happens at system shutdown/reboot, etc?