Crontab 切换背景失败 linux mint

Crontab 切换背景失败 linux mint

我编写了以下 crontab 任务,该任务应该在 linux mint 上每 10 分钟切换一次背景图像:

*/10 * * * * /home/me/Pictures/wallpapers/switcher.sh >> /home/me/Logs/wallpaper.log 2>&1

它调用这个 shell 脚本:

#!/bin/bash
gsettings set org.cinnamon.desktop.background picture-uri $(/usr/bin/python3 /home/me/Pictures/wallpapers/spaceBack.py)

在日志文件中我收到此错误消息:

(process:18951): dconf-CRITICAL **: 14:00:02.264: unable to create file '/home/me/.cache/dconf/user': Permission denied.  dconf will not work properly.
(process:18951): dconf-CRITICAL **: 14:00:02.265: unable to create file '/home/me/.cache/dconf/user': Permission denied.  dconf will not work properly.
(process:18951): dconf-CRITICAL **: 14:00:02.265: unable to create file '/home/me/.cache/dconf/user': Permission denied.  dconf will not work properly.
(process:18951): dconf-WARNING **: 14:00:02.265: failed to commit changes to dconf: Cannot autolaunch D-Bus without X11 $DISPLAY

我仅在通过运行 shell 脚本时收到此错误cron(通过终端运行正常)。

致电ls -la /home/me/.cache/dconf/ 退货

drwx------  2 root  root  4096 Jul  6 16:13 .
drwx------ 48 me me 4096 Jun 29 15:33 ..
-rw-------  1 root  root     2 Jul  6 16:13 user

答案1

DBUS_SESSION_BUS_ADDRESS必须设置为环境变量。可以通过以下脚本设置其正确的值,取自此问题如何通过远程 shell 更改 Gsettings?

#!/bin/bash

# Remember to run this script using the command "source ./filename.sh"

# Search these processes for the session variable 
# (they are run as the current user and have the DBUS session variable set)
compatiblePrograms=( nautilus kdeinit kded4 pulseaudio trackerd )

# Attempt to get a program pid
for index in ${compatiblePrograms[@]}; do
    PID=$(pidof -s ${index})
    if [[ "${PID}" != "" ]]; then
        break
    fi
done
if [[ "${PID}" == "" ]]; then
    echo "Could not detect active login session"
    return 1
fi

QUERY_ENVIRON="$(tr '\0' '\n' < /proc/${PID}/environ | grep "DBUS_SESSION_BUS_ADDRESS" | cut -d "=" -f 2-)"
if [[ "${QUERY_ENVIRON}" != "" ]]; then
    export DBUS_SESSION_BUS_ADDRESS="${QUERY_ENVIRON}"
    echo "Connected to session:"
    echo "DBUS_SESSION_BUS_ADDRESS=${DBUS_SESSION_BUS_ADDRESS}"
else
    echo "Could not find dbus session ID in user environment."
    return 1
fi

return 0

然后可以将 cron 作业修改为:*/10 * * * * DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /home/me/Pictures/wallpapers/switcher.sh >> /home/me/Logs/wallpaper.log 2>&1

相关内容