In my case I have to run openvpn
before ssh'ing into a server, and the openvpn
command echos out "Initialization Sequence Completed".
So, I want my script to setup the openvpn
and then ssh in.
My question is: How do you execute a command in bash in the background and await it to echo "completed" before running another program?
My current way of doing this is having 2 terminal panes open, one running:
sudo openvpn --config FILE
and in the other I run:
ssh SERVER
once the the first terminal pane has shown me the "Initialization Sequence Completed" text.
It seems like you want to run openvpn as a process in the background while processing its stdout in the foreground.
exec 3< <(sudo openvpn --config FILE)
sed '/Initialization Sequence Completed$/q' <&3 ; cat <&3 &
# VPN initialization is now complete and running in the background
ssh SERVER
Let's break it into pieces:
echo <(sudo openvpn --config FILE)
will print out something like /dev/fd63
<(..)
runs openvpn in the background, and...echo
exec 3< /dev/fd63
3
sed '/Initialization Sequence Completed$/q' <&3
3
we just openedcat <&3 &
3
and eventually block if nothing reads from itThe basic idea is to run openvpn
in the background, but capture its output somewhere so that we can run a command in the foreground that will block until it reads the magic words, "Initialization Sequence Completed". The above code tries to do it without creating messy temporary files, but a simpler way might be just to use a temporary file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With