2012-04-29 21 views
20

我寫了一個bash腳本來改變壁紙(對於GNOME3)。在終端仿真器執行cron與gsettings

#!/bin/bash 

# Wallpaper's directory. 
dir="${HOME}/images/wallpapers/" 

# Random wallpaper. 
wallpaper=`find "${dir}" -type f | shuf -n1` 

# Change wallpaper. 
# http://bit.ly/HYEU9H 
gsettings set org.gnome.desktop.background picture-options "spanned" 
gsettings set org.gnome.desktop.background picture-uri "file://${wallpaper}" 

腳本(例如侏儒末端)工程巨大。由cron執行,或ttyX終端收到錯誤期間:

** (process:26717): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n 

** (process:26717): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n 

** (process:26721): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n 

** (process:26721): WARNING **: Command line `dbus-launch --autolaunch=d64a757758b286540cc0858400000603 --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.\n 
+0

的解決方案都沒有爲我工作。 :(我必須在cron表達式中設置DISPLAY =:0.0之前(http://ubuntuforums.org/showthread.php?t=1023215)Ps我試圖運行一個使用pynotify的python腳本 – Hussain

+0

@ Hussain:我開始提問的時間已經過去了一段時間,我敢打賭下面的答案應該會更好 - 只是讀完整個討論。 –

回答

6

我找到了一些解決方案。當您導出文件〜/ .dbus/session-bus/*中包含的變量DBUS_SESSION_BUS_ADDRESS時,dbus-launch不會報告有關錯誤的更多信息。但是,而不是壁紙有文物。

添加代碼:

sessionfile=`find "${HOME}/.dbus/session-bus/" -type f` 
export `grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d'` 

現在腳本是這樣的:

#!/bin/bash 

# TODO: At night only dark wallpapers. 

# Wallpaper's directory. 
dir="${HOME}/images/wallpapers/" 

# Weird, but necessary thing to run with cron. 
sessionfile=`find "${HOME}/.dbus/session-bus/" -type f` 
export `grep "DBUS_SESSION_BUS_ADDRESS" "${sessionfile}" | sed '/^#/d'` 

# Random wallpaper. 
wallpaper=`find "${dir}" -type f | shuf -n1` 

# Change wallpaper. 
# https://superuser.com/questions/298050/periodically-changing-wallpaper-under-gnome-3/298182#298182 
gsettings set org.gnome.desktop.background picture-options "spanned" 
gsettings set org.gnome.desktop.background picture-uri "file://${wallpaper}" 
0

要通過的cron更改壁紙,只是這樣做,直接在crontab: 執行crontab -e命令

添加這樣的線:

30 09 * * * DIS PLAY =:0 GSETTINGS_BACKEND = dconf/usr/bin/gsettings set org.gnome.desktop.background picture-uri file:////home/elison/Pictures/morning.jpg

00 12 * * * DISPLAY = :0 GSETTINGS_BACKEND = dconf/usr/bin/gsettings set org.gnome.desktop.background picture-uri file:////home/elison/Pictures/noon.jpg

4

試過這個,它對我很好用:

dbus-launch --exit-with-session gsettings set schema key value 

或者從根的cron:

sudo -u user dbus-launch --exit-with-session gsettings set schema key value 

信用:http://php.mandelson.org/wp2/?p=565

+0

沒有任何反應,只是沒有錯誤顯示 – Sergey

+0

感謝它的工作! – grigio

+0

這有一個問題,如果你在一個交互式會話中使用它(例如'ssh'),系統上運行一個單獨的X11會話;它似乎會導致某種放緩或者可能的擊鍵捕獲,直到會話結束。「dbus」1.8版的手冊頁.18聲明:「要在文本中啓動一個D-Bus會話\(hymode會話,請不要使用dbus-launch,而應該參閱** dbus-run-session **(1)。」(我認爲怪異的'\('東西是一個格式錯誤。)不幸的是,似乎'dbus-run-session'是相當新的,因爲它在Debian 8但不是Debian 7. –

0

增加出口DISPLAY =:0 & &出口XAUTHORITY = /家庭/用戶名/ .Xauthority中,其中username是你的Ubuntu用戶名。它應該修復X11授權錯誤。

35

最後我管理如何解決這個問題後,許多嘗試。

事實上,問題的發生是因爲cron只使用了一組非常有限的環境變量。只有一個環境變量負責以正確的方式將腳本從設置爲cron作業的問題運行到DBUS_SESSION_BUS_ADDRESS而不是DISPLAYXAUTHORITYGSETTINGS_BACKEND或其他內容。這個事實在this answer也很好。

this answer問題是沒有保證是從~/.dbus/session-bus/目錄文件DBUS_SESSION_BUS_ADDRESS變量更新爲從當前的GNOME會話的電流值。爲了解決這個問題,一種方法是在當前的gnome會話中找到進程的PID,並從其環境中獲取dbus地址。我們可以做到這一點如下:

PID=$(pgrep gnome-session) # instead of 'gnome-session' it can be also used 'noutilus' or 'compiz' or the name of a process of a graphical program about that you are sure that is running after you log in the X session 
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-) 

話雖這麼說,腳本應該是這樣的:

#!/bin/bash 

# TODO: At night only dark wallpapers. 

# Wallpaper's directory. 
dir="${HOME}/images/wallpapers/" 

# export DBUS_SESSION_BUS_ADDRESS environment variable 
PID=$(pgrep gnome-session) 
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ|cut -d= -f2-) 

# Random wallpaper. 
wallpaper=`find "${dir}" -type f | shuf -n1` 

# Change wallpaper. 
# http://bit.ly/HYEU9H 
gsettings set org.gnome.desktop.background picture-options "spanned" 
gsettings set org.gnome.desktop.background picture-uri "file://${wallpaper}" 
+3

非常感謝。我有一個腳本,在午夜更新我的桌面壁紙。這個腳本幾個月前停止了正常工作。當我正常啓動它時,它工作正常,但是來自crontab並沒有完成這項工作。有了這個技巧,我可以解決這個問題。 – Jabba

+2

對於雙屏幕顯示器,將「spanned」選項更改爲「zoom」 – Coconop

+0

感謝此解決方案。我不得不稍微修改它以使用Debian上的gnome-shell:'PID = $(pgrep -o gnome-shell)'。 – scai