Saturday, May 4, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 2631  / 2 Years ago, fri, may 27, 2022, 7:11:53

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?


More From » command-line

 Answers
3

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

[#27539] Sunday, May 29, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tocklftime

Total Points: 110
Total Questions: 109
Total Answers: 100

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
;