Ubuntu – Starting Rhythmbox via crontab

cronrhythmbox

I'm getting tired of this issue now, because every version of Ubuntu needs to be treated in another way, but in 14.04 I don't know what to do anymore.

My problem:
My PC is starting automatically every day to act as my alert, because a crontab starts a script choosing a music to play, starts Rhythmbox, add the chosen file or list to play queue and plays.

The problem now is, the scripts works perfectly when I'm running it from a terminal, but started by crontab Rhythmbox is mostly not starting.

My crontab:

MAILTO=""
XAUTHORITY=~/.Xauthority
DISPLAY=:0.0

# m h  dom mon dow   command
*/10 * * * * /scripts/copy_loop.sh &

#start alert
50 6 * * 1 /scripts/alert.sh #mo
50 6 * * 2 /scripts/alert.sh #di
50 6 * * 3 /scripts/alert.sh #mi
50 6 * * 4 /scripts/alert.sh #do
50 6 * * 5 /scripts/alert.sh #fr
30 7 * * 6-7 /scripts/alert.sh #sa/so

The /script/copy_loop.sh script is another script which runs perfectly (but it doesn't have to start a program at all…)

alert-script:
#!/bin/bash

logfile="/scripts/log/alert.log"

exec >> $logfile 2>&1

echo "***********************************" 
echo "* Start ("`date +'%d.%m.%Y %H:%M'`") as `whoami` *" 
echo "***********************************" 

#To avoid heart-attacks ;)
amixer set Master 14

echo "Turning monitors on and waiting 5 seconds." 
#Force Monitor to turn on (grafik bug!)
xset dpms force on
sleep 5

#how many days should a song be blocked?
days=25

#Create array of music
music=( "Dubstep von Hannes/1-45 The Sound of Dubstep 4 - Mix 1.mp3" 
    "Purity Ring's Shrines.MP3" 
    "XXYYXX - XXYYXX (Full Album).MP3" 
    "Bonobo/Days to Come Disc 1/"
    "LIQUID STRANGER - BABYLON OUTCAST (DJ MIX).MP3"
    "Wisdom - Beautiful Chillstep.mp3"
    "Sizzlebird - Artist Mix.mp3"
    "Yann Tiersen - Amelie Soundtrack.mp3"
    "chillstep mix 2013 vol.3.mp3"
    "Burning Snowflakes Mix.mp3"
    "Parov Stelar - Daylight/"
    "Parov Stelar - Coco/Disc 1/"
    "Parov Stelar - Coco/Disc 2/"
    "Seeed/"
    "Salvation 2 Hour Best of Chillstep Mix.mp3"
    "Lonely Nights Wonderful Chillstep Mix.mp3"
    "Caribou - Swim (Full Album).mp3"
    "Gorillaz Plastic Beach 2010/"
    "Griechenland/Pendulum - Hold Your Colour/"
    "Griechenland/Pendulum In Silico/"
    "Pendulum - Immersion/"
    "Aphex Twin - Selected Ambient Works 85-92.MP3"
    "RJD2 - Deadringer (Full Album).MP3"
    "SOLAR FIELDS - RANDOM FRIDAY (ALBUM) HQ.MP3"
    "Deep Cuts/"
    "Silent Shout/"
    "Gorillaz/"
    "Kosheen/(2002) resist/"
    "Dub_Fx/Everythinks A Ripple/"
    "Griechenland/B-Complex/"
    )


for (( i=0; i < 1; i++ )); do
    choose=${music[$(($RANDOM % ${#music[@]}))]}

    #check if song is blocked
    cat /scripts/alertBlock | grep "$choose" > /dev/null

    if [[ $? == 0 ]]; then
        #song is blocked
        echo "$choose is blocked" 
        i=-1
    fi
done


#write to file, so it can't be played the next given days

lines=`wc -l < /scripts/alertBlock`

#cut the last entry
tail -n $(($days - 1)) /scripts/alertBlock > /scripts/.temp
cat /scripts/.temp > /scripts/alertBlock

echo "$choose" >> /scripts/alertBlock

echo "$choose chosen and added to Block-List" 

echo "waiting for Rhythmbox to start" 

rhythmbox-client --no-present & 

#ok=false

#while [[ $ok == false ]]; do
#    rhythmbox-client --check-running
#   
#   if [[ $? == 0 ]]; then
#       ok=true
#   else
#       ok=false
#   fi
#done

sleep 10

echo $choose | grep '/$'

if [[ $? == 0 ]]; then
    cd "/home/julian/Music/$choose"

    echo "Clearing queue" 
     rhythmbox-client --clear-queue 2>&1 

    # To avoid adding pictures and such crap
     rhythmbox-client --enqueue *.mp3
     rhythmbox-client --enqueue *.wav
     rhythmbox-client --enqueue *.ogg
     rhythmbox-client --enqueue *.flac
    sleep 2
     rhythmbox-client --play
else

     rhythmbox-client --no-start --play-uri="/home/julian/Music/$choose" 2>&1
fi

echo "Music player started. Have Fun!"  

echo "**************************" 
echo "* End ("`date +'%d.%m.%Y %H:%M'`") *" 
echo "**************************"

Basically the script turns on the monitors (works perfectly) adjust the volume (works perfectly) chooses a song and adds it to Block-List (works perfectly) and starts rhythmbox (meh… if it would work the add to playqueue and things would work too…)

In older distributens (12.10 until 13.10) Rhythmbox was starting (adding DISPLAY=:0.0 sudo -u julian or starting via rhythmbox-client --no-present) but now all these things don't work anymore.

The logfile generated when starting the script from terminal:

***********************************
* Start (28.05.2014 07:56) as julian *
***********************************
Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 31
  Mono: Playback 14 [45%] [-25.50dB] [on]
Turning monitors on and waiting 5 seconds.
RJD2 - Deadringer (Full Album).MP3 chosen and added to Block-List
waiting for Rhythmbox to start
Musicplayer startetd. Have Fun!
**************************
* End (28.05.2014 07:56) *
**************************

Rhythmbox starts and plays

started by crontab:

***********************************
* Start (28.05.2014 06:50) as julian *
***********************************
Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 31
  Mono: Playback 14 [45%] [-25.50dB] [on]
Turning monitors on and waiting 5 seconds.
Purity Ring's Shrines.MP3 chosen and added to Block-List
waiting for Rhythmbox to start

(rhythmbox-client:5655): GLib-GIO-CRITICAL **: g_dbus_proxy_new_sync: assertion 'G_IS_DBUS_CONNECTION (connection)' failed
Musicplayer startetd. Have Fun!
**************************
* End (28.05.2014 06:50) *
**************************

I see the error message here, but I don't know how to get rid of it.

Any help would be appreciated.

Edit:
To go one step deeper: There are two interesting questions for me:

  1. Why does every version behave different with its crontab?
  2. Sorry this sardonically question, but what is the reason it is not treated like a typed command of the user? It's obvious that one have to add the DISPLAY=:0.0 statement, but it's absolutely NOT obvious to add the xauthority file, moreover, it doesn't work anyaway.

If rhythmbox starts, then there are two possibilities:
1. It runs in an own instance, so rhythmbox-client doesn't see it.
2. It runs in an encapsulated instance of lightdm/unity, so the Hotkeys on the keyboard are not working and, if closed, the music plays on, but one have to kill it with killall rhythmbox because, starting Rhythmbox via unity in any way (quick-access in audio-panel too), creates a second instance.

I'm only guessing now, but I think, the last two points happens because crontab is doing his work not really in the name of the user it is set up.

Any help would be very appreciated.

Edit 2014-06-16:
To bring up more info: It seems like, that it isn't even possible to start any graphical interface in 14.04:

11 8 * * * gnome-terminal
11 8 * * * DISPLAY=:0.0 gnome-terminal

None of the above is working. I'm pretty sure in 13.04 and 13.10 this worked… So still investigating…

Best Answer

rhythmbox-client requires dbus to communicate with rhythmbox. You need to set the environment variable DBUS_SESSION_BUS_ADDRESS for rhythmbox-client. The value can be obtained from the environment with a script like this one.

touch $HOME/Xdbus
chmod 600 $HOME/Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/Xdbus

Run this script after you log in. Then in your alert.sh script you could source $HOME/Xdbus before you run rhythmbox-client.

Related Question