Solved rc.conf and cronjobs

I would like to run a script after OpenVPN made a connection to a server and if that script was executed successfully Transmission (torrent client) has to be started. But I have the following problem.
OpenVPN and Transmission are currently being executed by rc.conf and the script gets executed by cron at 60s after rebooting. That kinda works but sometimes the connection is not made and the script fails.

Now I'm not sure how to make a service wait until a cron job was executed successfully and how I could execute a cron job only if the OpenVPN connection was made. Is there a simple solution to this problem?
 
I would like to run a script after OpenVPN made a connection to a server
Have a look at the up/down scripts you can run.

Code:
       --up cmd
	      Run command cmd after successful TUN/TAP device open (pre	--user
	      UID change).

	      cmd consists of a	path to	script (or  executable	program),  op-
	      tionally	followed  by  arguments. The path and arguments	may be
	      single- or double-quoted and/or escaped using a  backslash,  and
	      should be	separated by one or more spaces.
openvpn(8)

Use that for a script to start Transmission, if you use service transmission onestart you don't have to enable it in rc.conf. That way it won't start when the machine boots.
 
  • Thanks
Reactions: dun
Thank you, that actually helps me :)

I have an issue though. How can I define the parameter for the --up flag within the rc.conf? Or is that not possible and I have to start OpenVPN without rc.conf?

My plan is now to start OpenVPN and then execute the script that eventually starts transmission-daemon.
 
How can I define the parameter for the --up flag within the rc.conf?
All the options can be set in your openvpn.conf too, that's the easiest way. Just remove the --, that's specifically for using the option on the command line.

My plan is now to start OpenVPN and then execute the script that eventually starts transmission-daemon.
Yes, that's the idea. The up script will be started when the connection is made, there's also an equivalent down script you can use to automatically stop Transmission when the connection goes down.
 
  • Thanks
Reactions: dun
All the options can be set in your openvpn.conf too, that's the easiest way. Just remove the --, that's specifically for using the option on the command line.

I added following line to the end of my openvpn.conf.
Code:
up "/usr/local/bin/bash -c '/usr/local/etc/openvpn/port_forwarding.sh >> /usr/local/etc/openvpn/port_forwarding_up.log'"

But when I run OpenVPN the script gets executed before I'm connected to the server. I tried to add a delay but that doesn't help because it just delays OpenVPN connecting to the server. I guess that's what --up-delay is there for but when I add up-delay before or after up the result stays the same.
 
I couldn't find a solution to this problem so I extended my script to check whether I'm still on my home address. It works now like I intended :)
Code:
check_connection( )
{
    IPINFO=`curl https://ipinfo.io 2>/dev/null`
    HOSTNAME=$(echo $IPINFO | jq -r '.hostname')
    while [[ "$HOSTNAME" == *my_provider ]]
    do
        sleep 1
        IPINFO=`curl https://ipinfo.io 2>/dev/null`
        HOSTNAME=$(echo $IPINFO | jq -r '.hostname')
        echo $HOSTNAME
    done

    if [[ "$HOSTNAME" != *my_provider ]]
    then
        port_forward_assignment
    fi
}
 
Back
Top