Linux-Mint

Crontab 在切換背景 linux mint 時失敗

  • July 7, 2020

我編寫了以下 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

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

引用自:https://unix.stackexchange.com/questions/596991