Apparently gsettings
needs some variables to be set. Because CRON uses only a very restricted set of environment variables you must set them before your script. Use the following code in your CRON line.
30 */2 * * * DISPLAY=:0 GSETTINGS_BACKEND=dconf /your/path/your-script.sh
In the example the job is set to run every 2 hours on the 30th minute. I've tried to insert the variables into the script, for a cleaner line, with no result, if someone find a way to do that, let us know.
Stumbled with these settings in ArchLinux forums.
The above solution no longer works with Vivid.
The best way to get this to work is indeed to find DBUS_SESSION_BUS_ADDRESS
variable, in the following script I'm using a for
loop to do the job because using pidof
of a specific application like gnome-session
doesn't always work for me and the newers applications have a different DBUS ADDRESS
probably because, in my particular case, I'm starting some daemons on boot with my user name. To effectively change the wallpaper I'm using dconf
but you can also use gsettings
. So tweak the script to your use case.
#!/bin/bash -e
user=$(whoami)
fl=$(find /proc -maxdepth 2 -user $user -name environ -print -quit)
while [ -z $(grep -z DBUS_SESSION_BUS_ADDRESS "$fl" | cut -d= -f2- | tr -d '\000' ) ]
do
fl=$(find /proc -maxdepth 2 -user $user -name environ -newer "$fl" -print -quit)
done
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS "$fl" | cut -d= -f2-)
if [ $# -gt 0 ]
then
PICS_PATH=$1
else
PICS_PATH="/home/public/Pictures/Wallpaper/"
fi
IMG=$(find -L $PICS_PATH -name "*.jpg" -o -name "*.png" | shuf -n1)
#gsettings set org.gnome.desktop.background picture-uri "file://${IMG}"
dconf write "/org/gnome/desktop/background/picture-uri" "'file://${IMG}'"
echo -e "$(date): ${IMG}" >> /tmp/wallch.log
in crontab add the following line to change wallpaper every even hour
0 */2 * * * /path/to-above-script.sh /path/to-wallpapers/
Your script is almost certainly running and creating your file. It's just not creating it where you expect it to be. You are not using any path in your script, you just use a file name. This means that the file will be created in the directory the script is running in.
That's why, when you move into a specific directory and run the script there, you think it works. Because the file is created in the directory you ran it in and so you can see it. By default, cron
runs in the home directory of the user running it. Since this is root's crontab (which, by the way, is a bad idea), the file will be created in /root
. So, go check:
sudo ls /root/test.txt
So, next time, give your script a full path instead of just a file name:
<?php file_put_contents ('/path/to/test.txt',"test content\n",FILE_APPEND); ?>
Now, the file will be created in /path/to
.
Best Answer
Editing gsettings from cron; missing environment variable
If you run the script from your own environment (e.g. from a terminal window or from Startup Applications), a number of environment variables will be set.
cron
however runs your script with a limited set of environment variables.To edit
gsettings
successfully fromcron
, you need to set theDBUS_SESSION_BUS_ADDRESS
environment variable. You can do that by adding two lines to your script, as described here (and below).Your script, including setting the needed variable
The script from here, edited to include the
DBUS_SESSION_BUS_ADDRESS
environment variable, then becomes:Related: Running .sh every 5 minutes