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?

  • You should export few more environment variables. Please read this answer. The solution provided there should do the job. For Lubuntu, just change $(pgrep gnome-session -n -U $UID) with $(pgrep lxsession -n -U $UID) (source). Another related topics: [1], [2], [3]. – pa4080 Mar 26 '18 at 12:42
  • 1
    @pa4080 I found the solution to my problem, using the guidance of your comment. Thank you. After some trial and error, I found that adding the variable export XDG_RUNTIME_DIR=/run/user/1000 in my script solves the problem. – Hedayat Mahdipour Mar 26 '18 at 14:31
  • Hello, Hedayat, just for fun I decided to rewrite your script in my manner :) – pa4080 Mar 26 '18 at 19:29
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.

  • 1
    I question why you have so many pictures of snails in your system. – Thomas Ward Mar 26 '18 at 19:38
  • @ThomasWard: I've got them a long ago and carry them with my $HOME through the years. Actually I don't know why :) – pa4080 Mar 26 '18 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.