Debian系统消息通知定制方法
notify-send是Debian系统最常用的命令行通知工具,适用于GNOME、KDE等主流桌面环境,可通过终端发送带标题、内容、图标和紧急程度的通知。
sudo apt install libnotify-bin(部分系统已预装)。notify-send "标题" "消息内容"(如notify-send "早餐提醒" "牛奶已热好")。-u low(低)、normal(默认)、critical(高);-i /path/to/icon.png(如notify-send -i /usr/share/icons/gnome/48x48/status/dialog-info.png "系统更新" "有2个安全更新可用");-t 5000(毫秒,默认5秒,设为0则保持显示直到手动关闭)。at命令实现延迟发送。例如,5分钟后发送提醒:echo "notify-send '会议提醒' '14:00有项目会议'" | at now + 5 minutes。通过自定义Bash脚本remind,可简化定时通知的管理(支持in/at时间格式、list/clear操作)。
~/bin/remind,并赋予执行权限(chmod +x ~/bin/remind):function remind() {
local COUNT="$#"
local COMMAND="$1"
local MESSAGE="$1"
local OP="$2"
shift 2
local WHEN="$@"
if [[ $COUNT -eq 0 || "$COMMAND" == "help" || "$COMMAND" == "--help" || "$COMMAND" == "-h" ]]; then
echo "Usage:"
echo " remind <message> <time> # Schedule a notification (e.g., 'remind \"Meeting\" in 10 minutes')"
echo " remind list # List scheduled reminders"
echo " remind clear # Clear all scheduled reminders"
return
fi
if ! command -v at &> /dev/null; then
echo "Error: 'at' utility is not installed. Install with 'sudo apt install at'."
return
fi
if [[ $COUNT -eq 1 ]]; then
if [[ "$COMMAND" == "list" ]]; then
at -l
elif [[ "$COMMAND" == "clear" ]]; then
at -r $(atq | cut -f1)
else
echo "Error: Unknown command '$COMMAND'. Use 'help' for usage."
fi
return
fi
local TIME=""
if [[ "$OP" == "in" ]]; then
TIME=$(date -d "$WHEN" +%H:%M)
elif [[ "$OP" == "at" ]]; then
TIME="$WHEN"
else
echo "Error: Invalid operator '$OP'. Use 'in' (e.g., 'in 5 minutes') or 'at' (e.g., 'at 14:30')."
return
fi
echo "notify-send \"$MESSAGE\"" | at "$TIME"
}
remind "喝水时间" in 10 minutes”;remind "下班回家" at 18:00”;remind list”/“remind clear”。通过修改系统文件,可自定义用户登录前后的提示信息。
/etc/issue(本地登录)或/etc/issue.net(SSH登录),添加自定义文本(如系统状态、公告)。例如:sudo nano /etc/issue
输入内容:\n=== Debian System ===\nLast update: $(date)\nVersion: $(lsb_release -ds)\n(\n表示换行,$(command)会动态执行命令)。/etc/motd(静态内容)或使用motd.tail(动态追加)。例如:sudo nano /etc/motd
输入欢迎信息:“Welcome to Debian Server!\nToday is $(date +%Y-%m-%d).”。对于更灵活的通知需求,可替换默认通知守护进程(如notify-osd或dunst)。
/etc/xdg/notify-osd/notifications.conf,可调整通知超时(timeout)、图标大小(icon_size)等参数。例如,将超时设为10秒:sudo nano /etc/xdg/notify-osd/notifications.conf
修改:timeout = 10000(单位:毫秒)。sudo apt install dunst;~/.config/dunst/dunstrc(需创建目录),可自定义通知位置(geometry)、颜色(color_normal)、优先级(priority)等。例如,设置通知显示在屏幕右上角:mkdir -p ~/.config/dunst
nano ~/.config/dunst/dunstrc
添加:geometry = "300x5-30+50"(格式:宽度x高度+X偏移+Y偏移)。通过编程语言(如Python)或第三方库,可实现更复杂的通知功能(如声音、弹窗、网络通知)。
sudo apt install python3-pynotify;import pynotify
n = pynotify.Notification("Python通知", "这是通过Python发送的提醒!", "dialog-information")
n.show()
运行:python3 your_script.py(需确保notify-send已安装)。/etc/systemd/system/notification.service,内容如下:[Unit]
Description=Custom Notification Service
After=network.target
[Service]
ExecStart=/usr/bin/notify-send "系统启动完成" "Debian系统已启动"
Restart=on-failure
[Install]
WantedBy=multi-user.target
启用并启动服务:sudo systemctl enable --now notification.service(开机自动发送通知)。