如何在bash中并排运行2行

时间:2018-07-20 01:59:16

标签: bash ubuntu ubuntu-16.04 notify-send

我是Bash的新手,我试图让此脚本在连接和断开与VPN的连接时通知我。

我遇到的问题是,当我运行“ openvpn”时,它将停止监听后面的其余行,因此我什至在登录之前都必须放置“已连接”通知行。是否有更理想的方法我可以这样写,以便我的“已连接”线仅在打开的vpn线已连接时才能运行?

如果有帮助,这适用于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 个答案:

答案 0 :(得分:1)

您可以附加&来从终端分离进程。否则,bash仅在openvpn退出时才继续执行脚本。

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

答案 1 :(得分:1)

您可能想让OpenVPN自己处理此通知。

来自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.

在配置文件中,这只是up /path/to/script。例如:

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

script-security位很重要,因为(同样来自手册页):

          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).

另请阅读有关--up-restart--down选项的信息。