how to run 2 lines on in bash side by side

76 Views Asked by At

I am new to Bash and I am trying to have this script notify me when I am connected and disconnected to my VPN.

The problem I have is when I run my "openvpn" it will stop listening to the rest of the lines that follow so I had to put my "connected" notification line before I even log in. Is there a more ideal way that I can write this so that my "connected" line will only run when the open vpn line has connected?

If it helps this is for Ubuntu.

#!/bin/bash

set -e

function discon {
  notify-send -i /usr/share/icons/Adwaita/32x32/devices/network-vpn.png "Home Network" "Disconnected"
}

notify-send -i /usr/share/icons/Adwaita/32x32/devices/network-vpn.png "Home Network" "Connected"

openvpn --config /home/matthew/Documents/vpn/MatthewLaptop.ovpn

trap discon EXIT
2

There are 2 best solutions below

1
On BEST ANSWER

You can append & to detach the process from the terminal. Otherwise bash will only continue the script when openvpn exits.

openvpn --config /home/matthew/Documents/vpn/MatthewLaptop.ovpn &

0
On

You probably want to have OpenVPN handle this notification itself.

From the OpenVPN man page:

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

           consists of  a  path  to  script  (or  executable  program),
          optionally  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.

In a configuration file, this is just up /path/to/script. For example:

user loval
group loval
script-security 2
up /home/loval/bin/vpn_is_up.sh

The script-security bit is important, because (also from the man page):

          0 -- Strictly no calling of external programs.
          1  -- (Default) Only call built-in executables such as ifconfig,
          ip, route, or netsh.
          2 -- Allow calling  of  built-in  executables  and  user-defined
          scripts.
          3  --  Allow passwords to be passed to scripts via environmental
          variables (potentially unsafe).

Also read about --up-restart and --down options.