
我有以下腳本來監視連線何時出現 => 使用特定網址開啟 chrome:
#!/bin/sh
function online {
wget -q -O /dev/null --timeout=5 http://URL/
return $?
}
until online
do
sleep 5
done
google-chrome --start-fullscreen --incognito "http://URL" &
現在我想監視連接是否丟失=>殺死chrome。劇本應該是什麼?
嘗試了下面的內容,但這不是正確的語法
#!/bin/sh
function offline {
wget -q -O /dev/null --timeout=5 http://URL/
return !$?
}
while offline
do
pkill chrome
sleep 5
done
答案1
我會擴展你的「啟動」腳本:
#!/bin/sh
url="http://URL/"
online() {
wget -q -O /dev/null --timeout=5 "$url"
}
# infinite loop
while :; do
# launch chrome when we go online
until online; do sleep 5; done
google-chrome --start-fullscreen --incognito "$url" &
# kill chrome when we go offline
while online; do sleep 5; done
pkill chrome
done
答案2
I have included both the conditions in script
wget - -spider “http:url”
If [[ $? == 0 ]]
Then
google-chrome --start-fullscreen --incognito "http://URL" &
Else
Ps -eaf | grep -i chrome | awk ‘{print “kill -9” “ “ $2}’ | sh