Tuesday, November 30, 2010

HowTo randomly change fluxbox wallpaper every n minutes

Some weeks ago' I've discovered this fantastic WM (Windows Manager), thanks to Gentoo/Sabayon + GNOME dbus sessions problems with my Imac. I desired so much to learn something about portage and the gentoo world that I accepted going on without gnome. Fortunatelly.
Fluxbox is a very light and high configurable WM for X server, super super fast and with all such things one need to work fine (alt-tab to move around opened windows, a nice bar showing opened applications and so on). It doesn't have icons by default, but has an amazing menu totally and easily configurable showing up when clicking on the desktop.
Here I want to show how is possible to set a cronjob task that changes randomly the desktop wallpaper every 5 minutes.
FIRST: install feh
feh is a fast, lightweight image viewer, supporting many image formats that we'll use to show the wallpaper. So
$sudo emerge feh
SECOND: try it out
Open the ~/.fulxbox/menu file and add some wallpaper voices, like
[submenu] (Wallpaper)
[exec] (tamars) {fbsetbg -f /home/abidibo/Wallpapers/sabayon_t.png}
[exec] (minus_tamars) {fbsetbg -f /home/abidibo/Wallpapers/sabayon.png}
[exec] (matrix) {fbsetbg -f /home/abidibo/Wallpapers/sabayon_matrix.jpg}
[exec] (winzoz) {fbsetbg -f /home/abidibo/Wallpapers/winzoz.jpg}
[exec] (toilet) {fbsetbg -f /home/abidibo/Wallpapers/toilet.jpg}
[exec] (linuxVSwin) {fbsetbg -f /home/abidibo/Wallpapers/linuxvswin.jpg}
fbsetbg is a wrapper that searches for a image viewer in order to set the fulxbox background (in our case it'll use feh, but other programs may be used).
Now clicking on the desktop and selecting a wallpaper voice from the menu should change our fluxbox wallpaper, if not maybe there were some problems during the installation of feh, try solving them and continue.
THIRD: make a script that changes the background randomly
So put all the desired images inside a folder and then copy this code in a file that we'll call chgWallpaper.sh
export DISPLAY=:0.0
fbsetbg -f /home/abidibo/Wallpapers/$(ls ~/Wallpapers | sort -R | tail -1)
Why we export the DISPLAY variable? Well, we always have to remember that the processes started by cron have no (or almost no) normal environment setup, and our script require this variable. So from terminal as normal user run
take the result and put it in place of :0.0
The second line of the script launches fbsetbg passing it one (tail -1) file randomly (sort -R) taken from the ~/Wallpapers directory (ls).
FOURTH: schedule the script with crontab
Now we have to schedule this script. My system has vixie-cron, many others uses crond, it's the same. We want to schedule the script as normal user (abidibo in my case), so
$sudo crontab -u abidibo -e
This commands opens your favourite editor, now we have to insert the job:
*/5 * * * * /bin/bash /home/abidibo/Scripts/chgWallpaper.sh
This way the script is run every 5 minutes. Now we save the file and exit and the new crontab is installed. We wait 5 minutes and our desktop background should change, if not go on reading.
If this doesn't work may be it happens something like what happened to me.
Read your system log file, in my case
$sudo vim /var/log/messages
and search for cron messages.
If you notice somithing like
Nov 30 14:35:01 localhost cron[7500]: (abidibo) CMD (bash /home/abidibo/Scripts/chgWallpaper.sh)
Nov 30 13:35:01 localhost postfix/postdrop[7511]: warning: unable to look up public/pickup: No such file or directory
Then you're welcome. Here we have a postfix problem, the pickup FIFO (files used by different processes to communicate) file is not present in your system, we have to create it, run
$sudo mkfifo /var/spool/postfix/public/pickup
$sudo /etc/init.d/postfix restart
And that should be fix it.
Now if it still doesn't work and your system log says something like
Nov 30 14:44:00 localhost postfix/local[7846]: fatal: open database /etc/mail/aliases.db: No such file or directory
you're welcome! It means you don't have that file. So you need to create it running the command
$sudo newaliases
$sudo /etc/init.d/postfix restart
Now cron may comunicate with you and send you emails that you may read by
$vim /var/mail/USERNAME
In my case cron was happy to say me such thing
From abidibo@abidibo-sabayon.localdomain  Tue Nov 30 15:29:01 2010
Return-Path: <abidibo@abidibo-sabayon.localdomain>
X-Original-To: abidibo
Delivered-To: abidibo@abidibo-sabayon.localdomain
Received: by abidibo-sabayon.localdomain (Postfix, from userid 1000)
         id 999DDD9E654; Tue, 30 Nov 2010 15:29:01 +0100 (CET)
From: root@abidibo-sabayon.localdomain (Cron Daemon)
To: abidibo@abidibo-sabayon.localdomain
Subject: Cron <abidibo@abidibo-sabayon> /bin/bash /home/abidibo/Scripts/chgWallpaper.sh
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/abidibo>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=abidibo>
X-Cron-Env: <USER=abidibo>
Message-Id: <20101130142901.999DDD9E654@abidibo-sabayon.localdomain>
Date: Tue, 30 Nov 2010 15:29:01 +0100 (CET)

Error: Can't open display:
That led me to add  the "export DISPLAY=:0.0" line in my script (thing that you've already done).
That's all. Hasta la proxima siempre!