You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 23.10启动管理器运行脚本时notify-send无通知问题

问题:开机启动脚本中notify-send通知不显示(Ubuntu 23.10)

问题描述

脚本可正常完成OneDrive挂载功能,但通过启动管理器开机运行时,notify-send无法弹出通知;直接在终端执行脚本或运行程序时,通知显示正常。已尝试设置DBUS_SESSION_BUS_ADDRESS但无效果。

相关脚本:

#!/bin/bash

// This was suggested in a post on stackoverflow, but still not doing nothing.
export DBUS_SESSION_BUS_ADDRESS="${DBUS_SESSION_BUS_ADDRESS:-unix:path=/run/user/${UID}/bus}"

# Error log file
ERROR_LOG_FILE="$HOME/onedrive_mount_error_log.txt"

# Delay to ensure the system and services are fully up
sleep 120  # Increased delay

# Explicitly set DBUS_SESSION_BUS_ADDRESS for notify-send in a startup environment
DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"
export DBUS_SESSION_BUS_ADDRESS

# Log the current environment variables (for debugging)
env >> "$HOME/script_env_log.txt"

# Check if rclone is installed
if ! command -v rclone &> /dev/null; then
    echo "rclone not found. Exiting script." >> $ERROR_LOG_FILE
    exit 1
fi

# Mount OneDrive using rclone
if rclone --vfs-cache-mode writes mount OneDrive: ~/OneDrive; then
    # If mount is successful, send a notification
    /usr/bin/notify-send "OneDrive Connected" "Microsoft OneDrive successfully mounted."
else
    # If mount fails, log the error
    echo "Failed to mount OneDrive on $(date)" >> $ERROR_LOG_FILE
    /usr/bin/notify-send "OneDrive Connection Failed" "Rclone failed to mount OneDrive."
fi

解决方案

方法1:动态获取当前会话的DBUS地址

启动管理器运行脚本时,可能无法正确关联活跃桌面会话的DBUS总线。替换脚本中的DBUS设置部分为以下内容,通过桌面进程获取正确的总线地址:

# 替换原有的DBUS_SESSION_BUS_ADDRESS设置逻辑
PID=$(pgrep -u $UID gnome-shell)
if [ -n "$PID" ]; then
    export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ | cut -d= -f2-)
else
    # 桌面进程未找到时, fallback到默认路径
    export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u)/bus"
fi

方法2:使用systemd用户服务(推荐)

systemd用户服务会在桌面会话完全启动后运行,自动继承正确的环境变量,稳定性更强:

  1. 创建服务文件~/.config/systemd/user/onedrive-mount.service:
[Unit]
Description=Mount OneDrive on login
After=graphical-session.target

[Service]
Type=simple
ExecStart=/full/path/to/your/script.sh
Restart=on-failure

[Install]
WantedBy=graphical-session.target

注意将/full/path/to/your/script.sh替换为脚本的绝对路径。
2. 启用并启动服务:

systemctl --user enable --now onedrive-mount.service

方法3:检查启动管理器的命令配置

确保启动管理器中的命令符合要求:

  • 使用绝对路径调用脚本,例如:/bin/bash /home/your-username/your-script.sh
  • 避免使用相对路径,确保脚本中$HOME等变量能正确解析

原因说明

notify-send依赖当前用户的DBUS会话总线发送通知,启动管理器在开机阶段运行时,可能早于桌面会话完全初始化,或进程未加入当前用户的会话环境,导致手动设置的总线路径无法正常工作。通过桌面进程获取总线地址或使用systemd用户服务,能确保脚本在正确的会话环境中执行。

内容的提问来源于stack exchange,提问作者Anima94

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 01:40:20