通过crontab启动Rhythmbox

我现在已经厌倦了这个问题,因为每个版本的Ubuntu需要以另一种方式对待,但是在14.04我不知道该怎么做了。

我的问题:我的电脑每天都自动启动以充当我的警报,因为crontab启动脚本选择要播放的音乐,启动Rhythmbox,添加所选文件或列表以播放队列和播放。

现在的问题是,当我从终端运行脚本时,脚本运行得很好,但是由crontab启动Rhythmbox大部分都没有启动。

我的crontab:

MAILTO="" XAUTHORITY=~/.Xauthority DISPLAY=:0.0 # mh 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 

/script/copy_loop.sh脚本是另一个完美运行的脚本(但它根本不需要启动程序……)

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  /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/.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 "**************************" 

基本上脚本打开监视器(完美地工作)调整音量(完美地工作)选择一首歌并将其添加到Block-List(完美地工作)并启动rhythmbox(meh …如果它可以起到添加到playqueue和东西的作用也会工作…)

在较旧的发行版(12.10至13.10)中,Rhythmbox正在开始(添加DISPLAY=:0.0 sudo -u julian或者通过rhythmbox-client --no-present ),但现在所有这些都不再适用了。

从终端启动脚本时生成的日志文件:

 *********************************** * 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开始播放

由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) * ************************** 

我在这里看到错误信息,但我不知道如何摆脱它。

任何帮助,将不胜感激。

编辑:更深入一步:我有两个有趣的问题:

  1. 为什么每个版本的crontab都表现不同?
  2. 对不起这个讽刺性的问题,但是它不被视为用户的类型命令的原因是什么? 显而易见的是,必须添加DISPLAY =:0.0语句,但添加xauthority文件绝对不是显而易见的,此外,它无论如何都无法正常工作。

如果rhythmbox启动,则有两种可能:1。它在自己的实例中运行,因此, rhythmbox-client看不到它。 2.它运行在lightdm / unity的封装实例中,因此键盘上的热键不起作用,如果关闭,音乐会播放,但是必须用killall rhythmbox杀死它,因为以任何方式通过统一启动Rhythmbox (也可以在音频面板中快速访问),创建第二个实例。

我现在只是猜测,但我认为,最后两点发生是因为crontab正在做的工作并非真正以它所设置的用户的名义。

任何帮助将非常感激。

编辑2014-06-16:提供更多信息:似乎,在14.04中甚至无法启动任何图形界面:

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

以上都不起作用。 我很确定在13.04和13.10这个工作……所以还在调查……

rhythmbox-client要求dbus与rhythmbox通信。 您需要为rhythmbox-client设置环境变量DBUS_SESSION_BUS_ADDRESS。 可以使用类似这样的脚本从环境中获取该值。

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

登录后运行此脚本。然后在alert.sh脚本中,您可以在运行rhythmbox-client之前获取$ HOME / Xdbus。

尝试在启动脚本之前导出变量:

 50 6 * * export XAUTHORITY && export DISPLAY=:0 && /scripts/alert.sh #mo ... 

记得通过输入以下内容来检查DISPLAY是否已连接到用户:

 w 

现在我选择了一种完全不同的方法来解决这个问题:

脚本现在等待一个fifo文件,直到来自crontab的命令接近然后开始工作。 因此,我可以在启动时启动脚本,并在crontab说这样做时使其工作。 现在显示管理器启动脚本而不是crontab现在看起来像:

 #start alert 50 8 * * 1 echo "start;`whoami`;'`date`'" > /scripts/alert/com_fifo #mo 30 7 * * 2 echo "start;`whoami`;'`date`'" > /scripts/alert/com_fifo #di ... 

我很抱歉,但是发布脚本几乎是不可能的,因为它在多个文件中被拆分以便能够重用某些function。 基本上它看起来像这样:

 hellNotFrozen=true com="/scripts/alert/com_fifo" #clear fifo file rm $com >/dev/null 2>&1 mkfifo $com while [[ $hellNotFrozen ]]; do #read fifo-file - this statement is blocking! inp=`cat $com` #$inp now holds the text pasted into the com-file #...do other stuff here rhythmbox-client --enqueue "/data/Music/$chosenFolder/*" rhythmbox-client --play done 

谢谢你的所有答案。