В настоящее время я пытаюсь реализовать этот скрипт, данный как принятый ответ на этот вопрос.
# 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"
Я создал новое задание в моем sudo crontab, как показано ниже:
0 * * * * /home/sharan/Daily-Reddit-Wallpaper/change_wallpaper_reddit.sh gnome > /home/sharan/Daily-Reddit-Wallpaper/cron.log 2>&1
Однако этот скрипт не работает и дает мне ошибки ниже в файле cron.log.
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
Я не совсем понимаю, что здесь происходит.
Вы запускаете задание cron как root, ожидая работы в домашнем каталоге пользователя. Проблема видна в этой строке:
LAST=$(cat "$HOME/$SHOME/last-desktop-environment.log")
, где $HOME переводится на /root/ и $SHOME на Daily-Reddit-Wallpaper. Затем cat жалуется, что нет файла /root/Daily-Reddit-Wallpaler/last-desktop-environment.log.
Вы запускаете задание cron как root, ожидая работы в домашнем каталоге пользователя. Проблема видна в этой строке:
LAST=$(cat "$HOME/$SHOME/last-desktop-environment.log")
, где $HOME переводится на /root/ и $SHOME на Daily-Reddit-Wallpaper. Затем cat жалуется, что нет файла /root/Daily-Reddit-Wallpaler/last-desktop-environment.log.