Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

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
share|improve this answer
    
cool. Thank u it worked just fine –  DarlekSupreme Jan 11 at 14:29
add comment

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

share|improve this answer
    
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? –  DarlekSupreme Jan 11 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 at 14:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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