使用注意事项:
1,脚本文件可执行
2,将switchbg.sh中的BGDIR设置为墙纸目录
3,在/etc/crontab中增加
*/5 * * * * test /home/test/switchbg.sh >/dev/null 2>&1
设置为每5分钟更换壁纸。
switchbg.sh
-------------------------------------------
#!/bin/bash
. ./discover_session_bus_address.sh
export DISPLAY=:0
confFile="/tmp/.switchbg.conf"
BGDIR="${HOME}/WallPaper"
filepath=${BGDIR}
find $filepath | grep -i -E ".jpg$|.png$|.jpeg$|.bmp$" > $confFile
cnt=`cat $confFile | wc -l`
while true
do
line=$(($RANDOM % $cnt + 1))
bgfile=$(head -n $line $confFile | tail -n 1)
bgfile="'file://$bgfile'"
bkfile=$(gsettings get org.gnome.desktop.background picture-uri)
# echo "old_wallpaper: $bkfile"
# echo "new_wallpaper: $bgfile"
if [ "$bkfile" != "$bgfile" ]
then
break
fi
done
gsettings set org.gnome.desktop.background picture-uri "$bgfile"
rm -f $confFile
-------------------------
discover_session_bus_address.sh
---------------------------------------------
#!/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