bash 如何检测网络何时在 /etc/init.d 脚本中初始化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20478516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:56:16  来源:igfitidea点击:

How to detect when networking initialized in /etc/init.d script?

bashshellubuntuinit.drc

提问by BayssMekanique

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.

我有一个 /etc/init.d/ 脚本,它在启动时启动并且需要网络到位。我将 rc start 设置为 99,所以它是加载的最后一件事,但网络不会立即加载,我目前不得不依赖 30 秒的 sleep 命令来等待网络加载。

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.

操作系统是针对嵌入式系统 (Yocto Project) 修改的 Ubuntu。文件系统是相同的,但服务非常有限,因此它使用 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 "" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: 
#!/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
{start|stop|status|restart}" exit 2 ;; esac

回答by hek2mgl

The basic Debian way:

Debian 的基本方式

Basically /etc/network/if-up.dis 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

基本上/etc/network/if-up.d是放置此类脚本的地方。当接口在 Debian 系统或衍生系统上启动时会调用这些脚本——比如 Ubuntu。在这里您可以找到传递给这些脚本的环境变量列表。使用这些变量,您可以决定启动哪个接口以及接口配置的详细信息。在查看时,您可能会找到此类 scipt 的示例/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 upstartwill be explained later)

一个示例脚本可能看起来像这样(源自 samba 包。只是为了解释事情是如何工作的。如果你使用的是 Ubuntu,你应该使用upstart将在后面解释)

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

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

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

Notethat you need to

请注意,您需要

start on net-device-up IFACE=eth0


Ubuntu / Upstart:

Ubuntu/新贵

Ubuntu is using upstartas a sysvinit replacement. If you are looking into /etc/network/if-up.d/you see that upstarthas placed a hook there as well. This hook is the base of the more elaborated upstartmechanism 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-upand passes the IFACE, LOGICAL, ADDRFAMand METHODas parameters. You use this hook in the upstart script for your service (like @Guy mentioned):

Ubuntu 正在upstart用作 sysvinit 的替代品。如果您正在查看,/etc/network/if-up.d/您会看到那里upstart也放置了一个钩子。这个钩子是更详细的upstart机制的基础,它提供了启动服务的可能性,除非某个网络接口已经启动。暴发户钩子将触发事件net-device-up,并传递IFACELOGICALADDRFAMMETHOD作为参数。您在服务的 upstart 脚本中使用此钩子(如提到的@Guy):

start on net-device-up IFACE=eth0

回答by Guy Gavriely

if you are on Ubuntu, as this question was tagged, consider using Ubuntu upstart

如果您使用的是 Ubuntu,因为此问题已标记,请考虑使用Ubuntu upstart

beside the great simplicity you'll also be able to:

除了非常简单之外,您还可以:

##代码##

for more info see entire upstart cookbook

有关更多信息,请参阅整个新贵食谱