Run cronjob as user to change desktop background i

2019-09-22 03:55发布

问题:

I am trying to run this script which changes my desktops background to a random picture in a directory. It works from the command line, and the cronjob gets run (added output and it gets spit out to a log file), but I can't get it to change my background. Here's my current line (set to run every minute for testing).

01 * * * * username /home/username/.wallpapers/flip.sh

Any Ideas?

回答1:

bug: https://bugs.launchpad.net/ubuntu/+source/gconf/+bug/285937

my version:

#!/bin/bash

# Script to randomly set Background from files in a directory

# Directory Containing Pictures
DIR=/home/lucas/studio/art/images/tapety
LOG=/home/lucas/tapeta.log

# Command to Select a random file from directory
PIC="$(
for p in [jJ][pP][gG] [pP][nN][gG] [sS][vV][gG] ; do
 ls $DIR/*.$p
done | shuf -n1
)"
# Command to set Background Image
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ] ; then
 # this is because of gconftool bug in cron
 TMP=~/.dbus/session-bus
 export $(grep -h DBUS_SESSION_BUS_ADDRESS= $TMP/$(ls -1t $TMP | head -n 1))
 echo $DBUS_SESSION_BUS_ADDRESS >> $LOG
fi

gconftool-2 -t string -s /desktop/gnome/background/picture_filename "$PIC"

and it's works on Fedora 12



回答2:

cron scripts have no access to the user's display. Use something like wallpapoz instead.



回答3:

01 * * * * /home/username/.wallpapers/flip.sh

means it runs every hour, if you want to run it every minute, it has to be

*/1 * * * * /home/username/.wallpapers/flip.sh

The syntax is also described on Wikipedia.

Or use GNOME Schedule as proposed.

Is flip.sh executable?

Edit: And true, the username has to be removed (in the system wide cron file, see silent's comment).Maybe this CronHowto also helps.



回答4:


I works great on my ubuntu hardy.

#!/bin/bash

# Script to change desktop background randomly every 5 mins through cronjob

FIND=/usr/bin/find
GCONFTOOL2=/usr/bin/gconftool-2
IMG_DIR=/home/username/Pictures/wallpapers

FILES=( `$FIND $IMG_DIR -iname '*.jpg'` )
TOTAL=${#FILES[@]}

# Select random number from 0 to $TOTAL
let INDEX=$RANDOM%TOTAL

$GCONFTOOL2 --type string --set /desktop/gnome/background/picture_filename "${FILES[${INDEX}]}"
$GCONFTOOL2 --type string --set /desktop/gnome/background/picture_options "centered"



Run it through cronjobs:

05 * * * * /home/username/scripts/wallpaper_switcher.sh



标签: bash shell cron