3

I have written a shell script which downloads random wallpaper from a web-page and sets it as a background every 5 hours. Here is the script:

#!/bin/bash

countervar=1;
finish=0;
cd /home/guinness/.rwallpaper/
if [ -f counter ]
then
    countervar=`cat counter`
fi

for (( i=$countervar; c<=2000000; i++ ))
do
   echo $i > "counter"
   if [ ! -f wallpaper-$i.jpg ]
   then
    wget "http://wallpapers.wallbase.cc/rozne/wallpaper-$i.jpg"

    if [ -f wallpaper-$i.jpg ]
    then
        gsettings set org.gnome.desktop.background picture-uri "file:///home/guinness/.rwallpaper/wallpaper-$i.jpg"
        exit
    fi
   fi
done

If I run a this script it works like a charm. I use cron to run this every 5 hours. The problem, is that it runs every 5 hours and successfully downloads the next image but it doesn't set it as a wallpaper. Can anybody tell me what can be the problem?

2 Answers 2

4

This is happen because cron uses only a very restricted set of environment variables. The only one environment variable that is responsible for running in the right way the script from the question when this is set as a cron job is DBUS_SESSION_BUS_ADDRESS.

So, you must to export DBUS_SESSION_BUS_ADDRESS environment variable in your script. See more explanations in my answer here.

In the end, your script should look like:

#!/bin/bash

PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-)

countervar=1;
finish=0;
cd /home/guinness/.rwallpaper/
if [ -f counter ]
then
    countervar=`cat counter`
fi

for (( i=$countervar; c<=2000000; i++ ))
do
   echo $i > "counter"
   if [ ! -f wallpaper-$i.jpg ]
   then
    wget "http://wallpapers.wallbase.cc/rozne/wallpaper-$i.jpg"

    if [ -f wallpaper-$i.jpg ]
    then
        gsettings set org.gnome.desktop.background picture-uri "file:///home/guinness/.rwallpaper/wallpaper-$i.jpg"
        exit
    fi
   fi
done
0
1

The problem you are encountering is almost certainly that the background switching part of your script relies on environment variables from your X session. Cron doesn't set any environment variables, so this will break this part of the script.

To get around the issue, you can put the script into a while loop with a sleep of roughly five hours and then call place your script in a location like /etc/X11/Xsession.d/my_script or ~/.xprofile

2
  • I have thought about this solution but wont the sleep function stop a whole thread?. And furthermore I don't really want to start the script by hand every time when I switch on the computer. So will this problem arise if I set this script as a start-up program? Jan 11, 2014 at 14:12
  • ~/.xprofile and /etc/X11/Xsession.d/xxx are both run automatically, so you won't have to start the script by hand. You can run the script in the background by adding an "&" in bash. So, in your ~/.xprofile you can add the line: "while true; do /path/to/my_script; sleep 999999; done &" And that will run your script once every so often.
    – xyrix
    Jan 11, 2014 at 14:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .