虚拟接口启动时运行脚本

虚拟接口启动时运行脚本

我已将 /sbin/ifup-local 设置为执行并且它可以工作,但我的虚拟接口 eth0.2 (vlan2) 是获取运行脚本所需的 IP 地址的接口。当 eth0.2 启动时,如何运行 bash 脚本?

我将 /sbin/ifup-local 重命名为 /sbin/ifup-eth0.2 但它不起作用

使用Centos6

答案1

您可以通过 initscript(/etc/init.d/ 下的脚本文件)轻松实现此目的,使用 LSB 标签“Required-Start”和“$network”参数:

### BEGIN INIT INFO
# Provides: After network script launching
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start a network dependent script
# Description: Network dependent script launcher
### END INIT INFO

使用骨骼这是你没有的。

然后修改“do_start()”函数来启动脚本,例如:

do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started

    cd /my/script/dir && ./myScript.sh

    # Add code here, if necessary, that waits for the process to be ready
    # to handle requests from services started subsequently which depend
    # on this one.  As a last resort, sleep for some time.
}

您还应该修改“do_stop()”函数以避免可能的关闭错误。

相关内容