想实现每隔30秒自动检测网络是否连通,并在桌面弹窗显示结果。
notify-send
用notify-send命令可以调出弹窗。
notify-send [OPTIONS] [body]
可以这样简单地调用 notify-send “标题” “内容”
如,
# notify-send "Network" "Connected."
sleep
要每30s检测一次,用sleep比较好。(at和cron都不咋靠谱)
用sleep 30s相当于程序延时30s。
脚本
#!/bin/bash
while true
do
ping -c1 -w1 www.baidu.com > /dev/null
if [ $? -eq 0 ]
then
notify-send "Connection" "Network Connected." -t 1000
echo "Ping Baidu succeed."
sleep 29.5s
else
notify-send "Connection" "Network Disconnected."
echo "Ping Baidu failed."
sleep 29s
fi
done