1

My script -

#!/bin/sh

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

DIR="/home/umang/Downloads/Wallpapers"
PIC=$(ls $DIR/*.jpg | shuf -n1)
/usr/bin/gsettings set org.gnome.desktop.background picture-uri file://$PIC

cronjob file -

* * * * * /path/to/script/Wallpaper_Changer.sh
* * * * * date >> /path/to/logfile/CronTest.log

Wallpaper changes correctly via terminal and the dates are logged via cron.

I am running ubuntu 14.04, GNOME Shell 3.12.1. Help me change wallpaper on gnome as well as unity.

Umang
  • 211
  • 3
  • 12

2 Answers2

1

There are quite a few questions that are similar to this on SO, a simple Google search yielded quite a few. There is this one:

Run cronjob as user to change desktop background in Ubuntu

This one: https://askubuntu.com/questions/25489/wallpaper-change-crontab-jobs-not-working-after-upgrade-from-8-04-to-10-04

This one: https://askubuntu.com/questions/403918/setting-cron-to-run-a-shell-script-random-wallpaper-from-a-webpage

And probably more. Hope one of these or others online help you out.

Community
  • 1
  • 1
julianwyz
  • 3,104
  • 1
  • 29
  • 34
1

I tried all the solutions but none helped. The problem was, I was still unable to access the environment variables(DBUS_SESSION_BUS_ADDRESS). So one could check if those are available first.

What worked for me is simply running the script in current shell environment. This can be done by running the script with a . as in cronjob file -

* * * * * . /path/to/script/Wallpaper_Changer.sh

More information : Global environment variables in a shell script Understanding Unix shells and environment variables

Community
  • 1
  • 1
Umang
  • 211
  • 3
  • 12