我在终端中运行脚本,但有时 wifi 断开连接,脚本停止工作。我大部分时间都不在笔记本电脑旁,那么有没有办法让我在 wifi 断开连接时用声音通知我?
此外帕特里克·特伦丁的解决方案。如果您想要这种脚本,可以尝试它的 python 版本。两者都很好用。这里:
答案1
一个非常简单的实现这一目标的脚本可能如下:
#!/bin/bash
PERIOD=10 # s.
WARNING_TEXT="Warning: the connection to SKYNET was lost."
LANGUAGE="en"
ICON="notification-network-wireless-disconnected"
# conn_monitor.sh:
# polls the connection state after PERIOD seconds, and reads aloud a warning message
# in case there is no connection
#
# dependencies:
# - sudo apt-get install espeak binutils libmad libnotify-bin
# NOTES:
# - nm-tool has been replaced by nmcli in newer versions of ubuntu (>= 15.04),
# see the output of `nmcli dev` to adapt this script to your needs.
function conn_monitor() {
while true :
do
sleep ${PERIOD}
mem_data=$(nm-tool | grep "State: connected")
if [[ -z "${mem_data}" ]]; then
notify-send "${WARNING_TEXT}" -i ${ICON} -u critical
paplay /usr/share/sounds/freedesktop/stereo/suspend-error.oga
espeak -a 200 -v ${LANGUAGE} "${WARNING_TEXT}"
fi
done
};
if [[ "$BASH_SOURCE" == "$0" ]]; then
conn_monitor $@
else
export -f conn_monitor
fi
然后可以添加一个名为的文件,conn_monitor.desktop
内容~/.config/autostart
如下:
[Desktop Entry]
Type=Application
Exec=..path..to..your..script..
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name=Conn Monitor
Comment=
其中您正确设置了脚本位置的路径。
我测试了脚本Ubuntu 14.04。