Anyone know anything about crontabs?

Discussion in 'all things UNIX' started by Trespasser, Nov 11, 2009.

Thread Status:
Not open for further replies.
  1. Trespasser

    Trespasser Registered Member

    Joined:
    Mar 1, 2005
    Posts:
    1,194
    Location:
    Virginia - Appalachian Mtns
    Hi,
    I've been trying to get a wallpaper changing bash script to run in cron but for some reason, no matter what I try, I can't get it to change my background. It shows in system log that cron is running the script but still no background change. Here's the wallpaper changer script....

    #############################
    #!/bin/bash

    export $(xargs -n 1 -0 echo </proc/$(pidof x-session-manager)/environ | grep -Z DBUS_SESSION_BUS_ADDRESS=)

    # Set the path to your wallpaper directory
    WALLPAPERS=/home/bobbyjean/Pictures

    # Sanity check. Is wallpapers directory empty of images?
    # + Build query
    QUERY=""
    if [ "$(ls $WALLPAPERS/*.jpg 2> /dev/null)" ]; then
    QUERY="$QUERY$WALLPAPERS/*.jpg "
    fi
    if [ "$(ls $WALLPAPERS/*.JPG 2> /dev/null)" ]; then
    QUERY="$QUERY$WALLPAPERS/*.JPG "
    fi

    # Sanity check. Does config directory exist?
    if [ ! -d /home/bobbyjean/.config/rotate-wallpaper ]; then
    mkdir /home/bobbyjean/.config/rotate-wallpaper
    fi

    # Sanity check. Does config file exist?
    if [ ! -f /home/bobbyjean/.config/rotate-wallpaper/count ]; then
    touch /home/bobbyjean/.config/rotate-wallpaper/count
    echo "0" > /home/bobbyjean/.config/rotate-wallpaper/count
    touch /home/bobbyjean/.config/rotate-wallpaper/number
    echo "0" > /home/bobbyjean/.config/rotate-wallpaper/number
    fi

    # Count number of wallpapers
    NO_OF_WALLPAPERS=0
    for file in $QUERY; do
    if [ ! -d "$file" ];then
    NO_OF_WALLPAPERS=$(( NO_OF_WALLPAPERS+1 ))
    fi
    echo "$NO_OF_WALLPAPERS" > /home/bobbyjean/.config/rotate-wallpaper/number
    done

    # Read in number of last wallpaper displayed, or start from 1
    PREVIOUS_WALLPAPER="$(cat /home/bobbyjean/.config/rotate-wallpaper/count)"
    NEW_WALLPAPER=$(( PREVIOUS_WALLPAPER+1 ))

    # Do we need to loop back to 1?
    if [ $NEW_WALLPAPER -gt $NO_OF_WALLPAPERS ];then
    NEW_WALLPAPER=1
    CNT=1
    fi

    # Set wallpaper
    for file in $QUERY; do
    if [ $CNT = $NEW_WALLPAPER ]; then
    if [ ! -d "$file" ];then
    /usr/bin/gconftool-2 -t str -s /desktop/gnome/background/picture_filename "$file"
    fi
    fi
    CNT=$(( CNT+1 ))
    done

    # Save count and exit
    echo "$NEW_WALLPAPER" > /home/bobbyjean/.config/rotate-wallpaper/count

    exit

    ############################

    and here is my crontab job.....

    SHELL=bin/bash
    */5 * * * * DISPLAY=:0.0 /home/bobbyjean/.chgwall/rotate-wallpaper.sh

    Anyone have any idea what the problem might be?

    Oh yeah, if I run this script from Terminal it changes the wallpaper just fine.

    Thanks for any help on this matter.

    BTW, this is in Karmic Netbook Remix 9.10.

    Later....
     
  2. Mrkvonic

    Mrkvonic Linux Systems Expert

    Joined:
    May 9, 2005
    Posts:
    8,698
    Are you sure your display is exported properly?
    BTW, your question has nothing to do with cron :) It's about a loooong script that does not quite do what you would have expected .... Check the cron exit status ... I'd wrap it in another script that dumps the env to see what is missing.
    Mrk
     
  3. Trespasser

    Trespasser Registered Member

    Joined:
    Mar 1, 2005
    Posts:
    1,194
    Location:
    Virginia - Appalachian Mtns
    Thanks Mrkvonic for the reply.

    I finally got it to work. I had to import the DBUS_SESSION_BUS_ADDRESS to get it to function properly. The script reads and numbers your wallpapers (only jpg) from your directory then displays them in sequence...not randomly. I found that using the random switch wasn't really all that random. If you wish, give it a try. Save it as "chgwall.sh" or whatever you want. Here's the revised script....

    Starts below this line......

    #!/bin/bash

    # Get the pid of nautilus
    nautilus_pid=$(pgrep -u $LOGNAME -n nautilus)

    # Grab the DBUS_SESSION_BUS_ADDRESS variable from nautilus's environment
    eval $(tr '\0' '\n' < /proc/$nautilus_pid/environ | grep '^DBUS_SESSION_BUS_ADDRESS=')

    # export it so that child processes will inherit it
    export DBUS_SESSION_BUS_ADDRESS

    # Set the path to your wallpaper directory here...mine was Pictures folder
    WALLPAPERS=~/Pictures

    # Sanity check. Is wallpapers directory empty of images?
    # + Build query
    QUERY=""
    if [ "$(ls $WALLPAPERS/*.jpg 2> /dev/null)" ]; then
    QUERY="$QUERY$WALLPAPERS/*.jpg "
    fi
    if [ "$(ls $WALLPAPERS/*.JPG 2> /dev/null)" ]; then
    QUERY="$QUERY$WALLPAPERS/*.JPG "
    fi

    # Sanity check. Does config directory exist?
    if [ ! -d ~/.config/rotate-wallpaper ]; then
    mkdir ~/.config/rotate-wallpaper
    fi

    # Sanity check. Does config file exist?
    if [ ! -f ~/.config/rotate-wallpaper/count ]; then
    touch ~/.config/rotate-wallpaper/count
    echo "0" > ~/.config/rotate-wallpaper/count
    fi

    # Count number of wallpapers
    NO_OF_WALLPAPERS=0
    for file in $QUERY; do
    if [ ! -d "$file" ];then
    NO_OF_WALLPAPERS=$(( NO_OF_WALLPAPERS+1 ))
    fi
    done

    # Read in number of last wallpaper displayed, or start from 1
    PREVIOUS_WALLPAPER="$(cat ~/.config/rotate-wallpaper/count)"
    NEW_WALLPAPER=$(( PREVIOUS_WALLPAPER+1 ))

    # Do we need to loop back to 1?
    if [ $NEW_WALLPAPER -gt $NO_OF_WALLPAPERS ];then
    NEW_WALLPAPER=1
    fi

    # Set wallpaper
    CNT=1
    for file in $QUERY; do
    if [ $CNT = $NEW_WALLPAPER ]; then
    if [ ! -d "$file" ];then
    gconftool-2 -t str -s /desktop/gnome/background/picture_filename "$file"
    fi
    fi
    CNT=$(( CNT+1 ))
    done

    # Save count and exit
    echo "$NEW_WALLPAPER" > ~/.config/rotate-wallpaper/count

    exit

    Ends with the above "exit"

    Then open Terminal and type...

    crontab -e

    Then paste this in...

    */5 * * * * ~/.chgwall/chgwall.sh

    As you can see I saved chgwall.sh to a hidden folder called .chgwall. I did this because I didn't want it showing every time I opened Home. If you do not want to save it to a similar folder then change the above line to whatever you wish.
     
    Last edited: Nov 12, 2009
  4. Mrkvonic

    Mrkvonic Linux Systems Expert

    Joined:
    May 9, 2005
    Posts:
    8,698
    I'm not into switching wallpapers ...

    But I guess there can be an easier way of doing it. Maybe a perl script revolving around:

    gconftool-2 -t str -s /desktop/gnome/background/picture_filename "$file"

    Then, just check for file existence before revolving, use generic names like pic-1, pic-2 etc and rand the number (mod with the number of images).

    It should be much shorter and cleaner.

    Something like ... ls the contents of a wallpaper dir.
    Rand the index, check if such file exists, if so, gconf it to wallpaper. Not sure why you need dbus thingie ...

    Mrk
     
  5. Trespasser

    Trespasser Registered Member

    Joined:
    Mar 1, 2005
    Posts:
    1,194
    Location:
    Virginia - Appalachian Mtns
    Random scripts are much, much shorter in length, but, like I said, none that I found were all that random. I would see the same wallpaper recycled quite frequently. That's why I prefer a script that displays them in sequence.

    As for the dbus part of the script I found that cron needed it to actually change the background. From what I understand Ubuntu changed cron a couple of editions back to where it's now required for anything related to the DISPLAY environment.

    Yes, it may be a long script...but it works. Knowing me, though, I'll probably start looking for ways to shorten it. I become so obsessed with this stuff. But it's fun...in a way. ;).

    Later...
     
  6. Trespasser

    Trespasser Registered Member

    Joined:
    Mar 1, 2005
    Posts:
    1,194
    Location:
    Virginia - Appalachian Mtns
    I'd like to make a correction to my statement.

    Again, if I recall correctly, devs made changes to gconf a couple of editions back that affected how cron handles gconftools-2, dbus and the DISPLAY environment.
     
  7. Ocky

    Ocky Registered Member

    Joined:
    May 6, 2006
    Posts:
    2,677
    Location:
    George, S.Africa
    Looks like you know more than you think you do. :p
    Wish I was that advanced - but am too Linux challenged - reluctant to burst out of my
    current 'comfort zone'. Nevertheless really enjoying Linux for work and play. It's my only OS.
     
  8. Trespasser

    Trespasser Registered Member

    Joined:
    Mar 1, 2005
    Posts:
    1,194
    Location:
    Virginia - Appalachian Mtns
    Thanks for the kind words, but I'm not advanced....only obsessed. When confronted with a problem I start looking for an answer (to my wife's dismay). I can spend the whole evening Goggling for a solution and not say a single word. My wife gets pissed and occasionally throws things at me :)dry:). But I'm happy to report that here recently I've learned to just "let-it-go", so to speak, if I can't find a solution after a few days. That was a big step for me and I feel SO much better.....at least I think I do.

    ;)

    Later...
     
  9. Ocky

    Ocky Registered Member

    Joined:
    May 6, 2006
    Posts:
    2,677
    Location:
    George, S.Africa
    :D know what you mean about the wife. The call to dinner can be rather disruptive..:argh:

    Ok I'm outta here - no more OT stuff -
     
Loading...
Thread Status:
Not open for further replies.