if-up.d 中的脚本似乎运行了 3 次。只想让它运行一次

Jef*_*son 1 networking linux debian openvpn debian-wheezy

我有一个脚本/etc/network/if-up.d可以在启动时连接到我的 VPN。该脚本似乎运行了 3 次,因此连接到我的 VPN 3 次。启动后,查看sudo ifconfig验证,实际上已经创建了三个隧道。另外,在查看 .txt 文件的输出时,我可以看到所有三个 openvpn 守护进程ps aux。如何确保它只运行一次?为什么它运行多次?我正在运行 Debian 7.3。

其内容为/etc/network/if-up.d/connectvpn

#!/bin/sh
/usr/sbin/openvpn --daemon --keepalive 5 30 --config /etc/openvpn/configs/bitterroot.conf
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助!杰夫

Sør*_*org 5

一般来说,ifup/ifdown并不能保证你的脚本只运行一次。因此你的脚本必须是幂等的;也就是说,包含自己的逻辑,不建立已经存在的隧道。当然,最好的方法是通过实际检查隧道是否已经存在来启动脚本:

\n\n
# Exit if network device "tun0" exists.\nip link show dev tun0 >/dev/null 2>&1 && exit 0\n
Run Code Online (Sandbox Code Playgroud)\n\n

此外,etc 中的脚本if-up.d会为每个启动的接口运行。\n正如 Jeff 所观察到的,脚本if-*.d不会接收任何参数来指定例如哪个接口即将启动。

\n\n

相反,根据Interfaces(5)手册页:

\n\n
   There  exists  for  each  of  the  above  mentioned options a directory\n   /etc/network/if-<option>.d/ [...]\n\n   All of these commands have access to the  following  environment  vari\xe2\x80\x90\n   ables.\n\n   IFACE  physical name of the interface being processed\n\n   LOGICAL\n          logical name of the interface being processed\n\n  [... and more ...]\n
Run Code Online (Sandbox Code Playgroud)\n\n

解决方案是启动脚本并检查正确的IFACE值,例如:

\n\n
   There  exists  for  each  of  the  above  mentioned options a directory\n   /etc/network/if-<option>.d/ [...]\n\n   All of these commands have access to the  following  environment  vari\xe2\x80\x90\n   ables.\n\n   IFACE  physical name of the interface being processed\n\n   LOGICAL\n          logical name of the interface being processed\n\n  [... and more ...]\n
Run Code Online (Sandbox Code Playgroud)\n\n

否则,该脚本将为每个网络设备运行,包括lo,此时建立隧道可能为时过早。

\n