Ubuntu的:等待网络连接起来,并执行bash命令 [英] Ubuntu: wait for network link up and execute a bash command

查看:835
本文介绍了Ubuntu的:等待网络连接起来,并执行bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ubuntu的(最新的发行版是罚款),我要重新启动路由器和bash脚本里面我想有一个等待网络链接会站起来,当它检测到一个命令,它已启动bash命令。

In Ubuntu (the latest distro is fine), I want to reboot a router and inside a bash script I'd like to have a command that waits for the network link to be up again and, when it detects that, it has to start a bash command.

我可以以某种轮询循环实现这一点,但理想的解决办法是有一个bash命令,在执行时,等待连​​接待涨,并自动执行,我给它一个bash命令。

I could implement this with some kind of polling loop, but the ideal solution would be to have a bash command that, when executed, waits for the link to be up and automatically executes a bash command that I gave to it.

我看了一些关于DBUS(和DBUS似乎要走的路),但它似乎也需要花费太多的时间来充分了解如何正确使用它。我建议检查是否像ethtool的一个工具,是能够做到那种等,并执行,但该名男子的网页我没有找到任何事情。

I read something about dbus (and dbus seems the way to go) but it also seems that it takes too much time to fully understand how to use it properly. I was suggested to check if a tool like ethtool was able to do that kind of "wait and execute" but in the man pages I didn't find anything about it.

请注意:我忘记说了,我想的命令检查链路的物理层了。在上层工作,所以解决方案是不能接受的。此外,涉及到把脚本里面的目录解决方案(如/ etc /网络/如果-up.d)不接受了。

Note: I forgot to say that I'd like the command to check if the PHYSICAL layer of the link is up. So solutions working at upper layers are not accepted. Moreover, solutions involving putting scripts inside directories (such as/etc/network/if-up.d) are not accepted too.

结果
任何想法?


Any ideas?

感谢您

推荐答案

事件监听器,我建议:

inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected'

在插入或拔出网线,这将引发回声更改检测,当然这可能会引发任何事情。

When you plug or unplug network cable, it will trigger echo 'Change detected', of course it could trigger just about anything.

和这将运行为一体了,但我带你知道如何使一个守护进程出来的,如果没有这将是一个很好的锻炼学习:)

And this will run as one off, but I take you know how to make a daemon out of it, if not it will be a good exercise to learn :)

这篇关于Ubuntu的:等待网络连接起来,并执行bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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