Showing posts with label cron. Show all posts
Showing posts with label cron. Show all posts

11 March 2011

Change wallpaper periodically in Ubuntu

Thanks to UbuntuForums.org, I made a simple cron job changing desktop wallpaper depending on time.

crontab

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
@hourly /share/scripts/bash/change_wallpaper.sh

change_wallpaper.sh

#!/bin/bash -
# Get the pid of nautilus
nautilus_pid=$(pgrep -u $LOGNAME -n nautilus)
# If nautilus isn't running, just exit silently
if [ -z "$nautilus_pid" ]; then
    exit 0
fi
# Grab the DBUS_SESSION_BUS_ADDRESS variable from nautilus's environment
eval $(tr '\0' '\n' < /proc/$nautilus_pid/environ | grep '^DBUS_SESSION_BUS_ADDRESS=')
# Check that we actually found it
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
    echo "Failed to find bus address" >&2
    exit 1
fi
# export it so that child processes will inherit it
export DBUS_SESSION_BUS_ADDRESS
HOUR=$(date +%H)
PICDIR="/usr/share/backgrounds/"
#echo "HOUR: $HOUR"
#echo "PICDIR: $PICDIR"
case $HOUR in
21|22|23|00|01|02|03|04|05|06)
/usr/bin/gconftool-2 --type string --set  /desktop/gnome/background/picture_filename $PICDIR"NATURE-ZenLizard.png"
;;
12|13|14|15|16|17|18|19|20)
/usr/bin/gconftool-2 --type string --set  /desktop/gnome/background/picture_filename $PICDIR"Fern_by_aalex04.jpg" 
;;
09|10|11)
/usr/bin/gconftool-2 --type string --set  /desktop/gnome/background/picture_filename $PICDIR"Bon-Echo_wallpaper.jpg"
;;
*)
/usr/bin/gconftool-2 --type string --set  /desktop/gnome/background/picture_filename $PICDIR"Ropey_Photo_by_Bob_Farrell.jpg"
;;
esac

I wonder, why it didn't work without DBUS_SESSION_BUS_ADDRESS detection. However, it works now. I'd be glad, if it helps someone.

17 December 2010

Anacron for a user

Normally, anacron is launched by root. And here is how a user can launch his/her own anacron jobs.

Prepare file and dirs

$ mkdir -p ~/etc/cron.daily ~/etc/cron.weekly ~/etc/cron.monthly \
~/var/spool/anacron
$ cp /etc/cron.daily/0anacron ~/etc/cron.daily/
$ cp /etc/cron.daily/.placeholder ~/etc/cron.daily/
$ cp /etc/cron.weekly/0anacron ~/etc/cron.weekly/
$ cp /etc/cron.weekly/.placeholder ~/etc/cron.weekly/
$ cp /etc/cron.monthly/0anacron ~/etc/cron.monthly/
$ cp /etc/cron.monthly/.placeholder ~/etc/cron.monthly/
$ find ~/etc/cron.* -name 0anacron -exec chmod u+x {} \;

~/etc/anacrontab

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=user@domain.com
LOGNAME=anacron

1   5   cron.daily   nice run-parts --report /etc/cron.daily
7   10  cron.weekly  nice run-parts --report /etc/cron.weekly
@monthly    15  cron.monthly nice run-parts --report /etc/cron.monthly
Replace MAILTO value with your mailbox. If mailx package installed, you should receive anacron job error outputs by E-mail.

Make anacron launched on login

$ echo "
/usr/sbin/anacron -t \$HOME/etc/anacrontab -S \$HOME/var/spool/anacron
" >> ~/.bash_profile
UPDATE 20 DECEMBER 2010
Note, if ~/.profile exists, ~/.bash_profile may be ignored(as in my case). I wonder, why? Bash man page bash(1) claims:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
Put your scripts in ~/etc/cron.daily, ~/etc/cron.weekly and ~/etc/cron.monthly directories. Note, that script file names must consist entirely of upper and lower case letters, digits, underscores, and hyphens, since we launch them through run-parts(see man run-parts 8). In case of custom fienames you should apply --regex option.