1

I have scheduled a job in crontab to change the desktop background randomly, every minute in Lubuntu, using the following script:

#!/bin/bash
export DISPLAY=:0
PHOTOS_PATH=~/Pictures/wallpapers/
number_of_photos=$(ls $PHOTOS_PATH | wc -l)

# generates a random number in range 1 through $number_of_photos
random=$( shuf -i 1-$number_of_photos -n 1 )

# returns the name of the file at line number $random of `ls` output
photo_file=$(ls $PHOTOS_PATH | head --lines=$random | tail --lines=1)

pcmanfm --set-wallpaper=$PHOTOS_PATH/$photo_file
exit 0

But every minute, the following error message appears on the screen:

enter image description here

The problem is with the line of the script that issues command pcmanfm, since (according to my experiments) this message appears exactly at the execution of that command. I've also run this script from inside tty1, and it changed my desktop background successfully, with no error. How can I overcome this problem with crontab?

1 Answers1

0

Here is my version of your script. By this approach we do not need to worry which environment variable we should export, because we exporting all available variables for the current user's session.

#!/bin/bash

# NAME: lubuntu-wp-changer

# Initial variables
ITEMS_PATH="$HOME/Pictures/wallpapers"
ITEMS=("$ITEMS_PATH"/*)

# Check whether the user is logged-in, if yes export the current desktop session environment variables
[ -z "$(pgrep lxsession -n -U $UID)" ] && exit 0 || export $(xargs -0 -a "/proc/$(pgrep lxsession -n -U $UID)/environ") >/dev/null

# Generates a random number in the range determinated by the number of the items in the array ${ITEMS[@]}
ITEM=$(( ($RANDOM) % ${#ITEMS[@]} ))

# Set the wallpaper
pcmanfm --set-wallpaper="${ITEMS[$ITEM]}"

exit 0

Here is my Cronjob that changing the wallpaper every three seconds:

* * * * * bash -c 'for i in {1..20}; do $HOME/lubuntu-wp-changer; sleep 3; done'

Here is the result:

enter image description here

More details could be found in my GitHub project: cron-gui-launcher.

pa4080
  • 30,621