我已经将 Empathy 添加到默认打开的应用程序列表中,它被配置为在启动时自动连接到 MSN,但是当我登录到我的笔记本电脑时,wifi 连接需要几秒钟才能准备好。还没上网,Empathy就已经启动了,尝试登录MSN失败,之后就连接不上了。
这似乎是 Empathy 中的一个错误,但是我如何才能修复它,或者如果不可能,我该如何延迟它的启动直到网络启动?
显然这是 Empathy 中的一个已知错误,所以我决定从一个脚本启动 Empathy,该脚本检查网络是否已启动(连接到http://www.google.com,互联网的真正心跳 :)如果网络不工作,它将休眠 5 秒并重试,直到尝试 30 次
这是脚本(名为waitfornet.py)
#!/usr/bin/python
from urllib2 import urlopen, URLError
from subprocess import Popen
from time import sleep
from sys import argv
MAX_TRIES = 30
DELAY = 5
if len (argv) < 2:
print ('Check for network connectivity and run a command once the net is up')
print ('Tries up to %d times waiting %d seconds between each try' % (MAX_TRIES, DELAY))
print ('\nUSAGE: python waitfornet.py <command to run>')
else:
while True:
MAX_TRIES -= 1
if MAX_TRIES < 0:
raise ValueError ('Reached the max iteration count and the net is still down')
try:
data = urlopen('http://www.google.com')
except URLError:
# if there's a problem connecting to google, that must mean
# that the net is still down, so sleep 5 seconds and try again
print ('Internet is down... retrying...')
sleep (DELAY)
continue
# if you got here it means that the urlopen succeded
pid = Popen([argv[1], ' '.join(argv[1:])]).pid
break
Run Code Online (Sandbox Code Playgroud)
这就是我从“启动应用程序”菜单启动它的方式:
~/scripts/waitfornet.py empathy
Run Code Online (Sandbox Code Playgroud)