I am currently trying to implement this script given as the accepted answer on this question. Now I have created the script change_wallpaper_reddit.sh
, and the script is below for your convenience.
# Reference: https://askubuntu.com/a/911958/566421
# Set the script home directory:
SHOME=Daily-Reddit-Wallpaper
# Set the output folder in the home directory to save the Wallpapers to:
DIR=Pictures/Wallpapers
# Set the --time parameter value
TIME=now
# Check if the Desktop Environment is changed:
LAST=$(cat "$HOME/$SHOME/last-desktop-environment.log")
if [ "$1" != "$LAST" ]
then
# Get the name of the last saved wallpaper image:
IMG=$(ls -Art $HOME/$DIR | tail -n 1)
rm $HOME/$DIR/$IMG
fi
# Desktop Environment cases:
if [ -z ${1+x} ] || [ "$1" = "gnome" ] || [ "$1" = "unity" ]
then
# Set the necessary environment variables - PID=$(pgrep gnome-session -u $USER) - UBUNTU/UNITY/GNOME:
export GNOME_DESKTOP_SESSION_ID=true
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep gnome-session -n)/environ | cut -d= -f2-)
# Run the script:
~/$SHOME/change_wallpaper_reddit.py --time $TIME --output ~/$DIR
elif [ "$1" = "kde" ]
then
# Set the necessary environment variables - KUBUNTU/PLASMA/KDE:
export KDE_FULL_SESSION=true
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep startkde -n)/environ | cut -d= -f2-)
# Run the script:
$HOME/$SHOME/change_wallpaper_reddit.py --time $TIME --output $DIR
elif [ "$1" = "mate" ]
then
# Set the necessary environment variables - Ubuntu MATE/MATE:
export DESKTOP_SESSION=mate
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep mate-session -n)/environ | cut -d= -f2-)
# Run the script:
$HOME/$SHOME/change_wallpaper_reddit.py --time $TIME --output $DIR
elif [ "$1" = "lxde" ]
then
# Set the necessary environment variables - type 'echo $DISPLAY` to find your current display - LUBUNTU/LXDE:
export DESKTOP_SESSION=Lubuntu
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep lxsession -n)/environ | cut -d= -f2-)
export DISPLAY=:1.0
# Run the script:
$HOME/$SHOME/change_wallpaper_reddit.py --time $TIME --output $DIR
elif [ "$1" = "xfce4" ]
then
# Set the necessary environment variables - XUBUNTU/XFCE4:
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep xfce4-session -n)/environ|cut -d= -f2-)
# Run the script:
$HOME/$SHOME/change_wallpaper_reddit.py --time $TIME --output $DIR
# Get the name of the last saved wallpaper image:
IMG=$(ls -Art $HOME/$DIR | tail -n 1)
# Since 'change_wallpaper_reddit.py' doesn't work properly with xfce4 we shall set the background manually:
xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor0/workspace0/last-image --set $HOME/$DIR/$IMG
# Property list: xfconf-query --channel xfce4-desktop --list
# Current settings: xfconf-query -c xfce4-desktop -p /backdrop -lv
# Set 'zoomed' style: xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor0/workspace0/image-style --set 5
# References: https://askubuntu.com/q/380550/566421 and https://askubuntu.com/q/414422/566421
else
echo "Wrong argument. It must be:"
echo " - empty (default) = gnome = unity"
echo " - kde"
echo " - lxde"
echo " - mate"
echo " - xfce4"
fi
# Save the current value of the Desktop Environment variable:
echo "$1" > "$HOME/$SHOME/last-desktop-environment.log"
I have created a new job in my sudo crontab
, as shown below:
0 * * * * /home/sharan/Daily-Reddit-Wallpaper/change_wallpaper_reddit.sh gnome > /home/sharan/Daily-Reddit-Wallpaper/cron.log 2>&1
However, this script isn't working and gives me the errors below, in the cron.log
file.
cat: /root/Daily-Reddit-Wallpaper/last-desktop-environment.log: No such file or directory
ls: cannot access '/root/Pictures/Wallpapers': No such file or directory
rm: cannot remove '/root/Pictures/Wallpapers/': No such file or directory
/home/sharan/Daily-Reddit-Wallpaper/change_wallpaper_reddit.sh: 29: /home/sharan/Daily-Reddit-Wallpaper/change_wallpaper_reddit.sh: /root/Daily-Reddit-Wallpaper/change_wallpaper_reddit.py: not found
/home/sharan/Daily-Reddit-Wallpaper/change_wallpaper_reddit.sh: 88: /home/sharan/Daily-Reddit-Wallpaper/change_wallpaper_reddit.sh: cannot create /root/Daily-Reddit-Wallpaper/last-desktop-environment.log: Directory nonexistent
I don't exactly understand what is going wrong here.
You run the cron job as root, while expecting to work within a user's home directory. The problem is visible in this line:
where
$HOME
gets translated to/root/
and$SHOME
toDaily-Reddit-Wallpaper
. Thencat
complains that there is no file/root/Daily-Reddit-Wallpaler/last-desktop-environment.log
.