如何当在/etc/init.d脚本联网初始化检测? [英] How to detect when networking initialized in /etc/init.d script?

查看:249
本文介绍了如何当在/etc/init.d脚本联网初始化检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有开始在引导和需要联网到位的/etc/init.d/的脚本。我有RC起点设置为99,所以它的负荷,但网络没有立即加载的最后一件事,我现在不得不依靠30秒睡眠命令等待网络负载。

操作系统Ubuntu的是修改为嵌入式系统(Yocto项目)。文件系统是相同的,但也有极有限的服务,所以它使用的Sysvinit。

有没有一种方法来检查所有网络已经完成加载?

下面是脚本,如果它是的帮助:

 #!/ bin / sh的

#守护程序的名称:blerd

#chkconfig的: - 3 99 1
#描述:M3低功耗蓝牙接力守护#源函数库。
。 /etc/init.d/functions程序= BLER
锁定文件=的/ var /锁/ SUBSYS / $ PROGRAM启动(){
    [-x $ EXE] || 5号出口    呼应-n启动程序$;    如果[-e $锁文件]然后
        回声$程序的背景存在。
        返回0;
    科幻    睡眠30
    $ PROGRAM
    RETVAL = $?
    如果[$ RETVAL -eq 0];然后
        回声成功
    其他
        回声失败
    科幻    [$ RETVAL -eq 0]放大器;&安培;触摸$锁定文件
    返回$ RETVAL
}停止() {
    呼应-n停止$ PROGRAM    如果[! -e $锁定文件]然后
        回声$程序没有实例存在。
        返回0;
    科幻    killproc $ PROGRAM
    RETVAL = $?
    如果[$ RETVAL -eq 0];然后
        回声成功
    其他
        回声失败
    科幻    [$ RETVAL -eq 0]放大器;&安培; RM -f $锁定文件
    返回$ RETVAL
}状态() {
    呼应-n状态$ PROGRAM
    如果[-e $锁文件]然后
        回声暗战
    其他
        回声停止
    科幻
    返回0;
}#看看我们是如何调用。
案$ 1
    开始)
        开始
        ;;
    停止)
        停止
        ;;
    重新开始)
        停止
        开始
        ;;
    状态)
        状态
        ;;
    *)
        回声用法:$ {0开始|停止|状态|重启}
        2号出口
        ;;
ESAC


解决方案

Debian的基本方法

基本上 /etc/network/if-up.d 是这样的脚本的地方。这些脚本调用时的界面得到的一个Debian系统或衍生物上 - 比如Ubuntu。在这里,你可以找到传递给这些脚本的环境变量列表。使用这些变量,你可以决定哪些接口,可以获取的和接口的配置的详细信息。寻找到 /etc/network/if-up.d

当你可能会发现这样的scipts的例子

一个示例脚本看起来是这样的(从桑巴包的。只是为了解释事情是如何工作的。如果你正在使用Ubuntu,你应该使用新贵将在后面解释)

/etc/network/if-up.d/your-service

 #!/ bin / sh的
#尝试把NMBD起来,当一个接口出现,如果SMBD已在运行。#不要刻意去为LO做任何事情。
如果[$ IFACE= LO];然后
    退出0
科幻#仅从ifup的运行。
如果[!$ MODE=开始];然后
    退出0
科幻#我们只关心INET和INET6。
万一$ ADDRFAM
    INET | INET6 | NetworkManager的)
        ;;
    *)
        退出0
        ;;
ESAC
#启动服务
调用-的rc.d您的服务开始>的/ dev / null的退出0

注意您需要

 搭配chmod + X /etc/network/if-up.d/your-service


的Ubuntu /新贵

Ubuntu是使用新贵作为sysvinit的替代品。如果您正在寻找进入 /etc/network/if-up.d / 您将看到新贵已经把一个钩子有作为。这个钩子是更加完备新贵的基础,提供了启动服务不会,除非某个网络接口已经长大的可能性机制。新贵钩子将触发事件网​​络设备向上并传递 IFACE 逻辑 ADDRFAM 方法作为参数。您可以使用此挂钩的新贵脚本为您服务(如@Guy提及):

 净设备开始上升的IFACE = eth0的

I have an /etc/init.d/ script that starts on boot and requires networking to be in place. I have the rc start set to 99, so it's the last thing that loads, but the networking doesn't load immediately and I'm currently having to rely on a 30 second sleep command to wait for network loading.

The OS is Ubuntu modified for embedded systems (Yocto Project). The filesystem is the same, but there are very limited services so it's using Sysvinit.

Is there a way to check that all networking has finished loading?

Here is the script if it is of help:

#!/bin/sh
#
# Daemon Name: blerd
#  
# chkconfig: - 3 99 1
# description: M3 Bluetooth Low Energy Relay Daemon

# Source function library.
. /etc/init.d/functions

PROGRAM=bler
LOCKFILE=/var/lock/subsys/$PROGRAM

start() { 
    [ -x $EXE ] || exit 5

    echo -n "Starting $PROGRAM: ";

    if [ -e $LOCKFILE ]; then
        echo "Instance of $PROGRAM exists."
        return 0;
    fi

    sleep 30
    $PROGRAM
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        echo "Success"
    else
        echo "Failed"
    fi

    [ $RETVAL -eq 0 ] && touch $LOCKFILE
    return $RETVAL
}

stop() {
    echo -n "Stopping $PROGRAM: "

    if [ ! -e $LOCKFILE ]; then
        echo "No instance of $PROGRAM exists."
        return 0;
    fi

    killproc $PROGRAM
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        echo "Success"
    else
        echo "Failed"
    fi

    [ $RETVAL -eq 0 ] && rm -f $LOCKFILE
    return $RETVAL
}

status() {
    echo -n "Status $PROGRAM: "
    if [ -e $LOCKFILE ]; then
        echo "Running"
    else
        echo "Stopped"
    fi
    return 0;
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 2
        ;;
esac

解决方案

The basic Debian way:

Basically /etc/network/if-up.d is the place for such scripts. These scripts are called when an interface get's up on a Debian system or derivate - like Ubuntu. Here you can find a list of environment variables passed to these scripts. Using these variables you can decide which interface get's up and details of the interface's configuration. You may find examples of such scipts when looking into /etc/network/if-up.d

A sample script could look like this (derived from samba package. Just to explain how things work. If you are using Ubuntu you should use upstart will be explained later)

/etc/network/if-up.d/your-service

#!/bin/sh
# Try to bring nmbd up when an interface comes up, if smbd is already running.

# Don't bother to do anything for lo.
if [ "$IFACE" = lo ]; then
    exit 0
fi

# Only run from ifup.
if [ "$MODE" != start ]; then
    exit 0
fi

# we only care about inet and inet6.
case $ADDRFAM in
    inet|inet6|NetworkManager)
        ;;  
    *)  
        exit 0
        ;;  
esac


# start the service
invoke-rc.d your-service start >/dev/null

exit 0

Note that you need to

chmod +x /etc/network/if-up.d/your-service


Ubuntu / Upstart:

Ubuntu is using upstart as a sysvinit replacement. If you are looking into /etc/network/if-up.d/ you see that upstart has placed a hook there as well. This hook is the base of the more elaborated upstart mechanism that provides the possibility to start a service not unless a certain network interface has been brought up. The upstart hook will trigger the event net-device-up and passes the IFACE, LOGICAL, ADDRFAM and METHOD as parameters. You use this hook in the upstart script for your service (like @Guy mentioned):

start on net-device-up IFACE=eth0

这篇关于如何当在/etc/init.d脚本联网初始化检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆