DBUS_SESSION_BUS_ADDRESS for script called from crontab.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting DBUS_SESSION_BUS_ADDRESS for script called from crontab.
# 1  
Old 10-15-2012
DBUS_SESSION_BUS_ADDRESS for script called from crontab.

Hi,

I'm writing a Bash script for wallpaper clocks which works fine on the command line but needs to be run every minute from a crontab entry so the time gets changed on the wallpaper (the wallpaper gets rebuilt every minute so the current weekday, date, month, hour, and minute become part of the wallpaper).

The script changes the wallpaper like this, depending on which version of Gnome is running:

Code:
Gnome v.2:
gconftool-2 -t string -s /desktop/gnome/background/picture_filename "$currentImagePath"

Gnome v.3:
gsettings set org.gnome.desktop.background picture-uri "file://$currentImagePath"

The change wallpaper commands above run fine from the command line but when the script is run from crontab it seems the DBUS_SESSION_BUS_ADDRESS environment variable must be set otherwise the wallpaper does not get changed (although all other aspects of the script work fine).

Is there a generic way to get and set the value of DBUS_SESSION_BUS_ADDRESS from within the script which will work with both Gnome v.2 and v.3 and regardless of which flavour of Unix/Linux is running?

I've found 2 methods on the web:

Code:
nautilus_pid=$(pgrep -u $LOGNAME -n nautilus)
eval $(tr '\0' '\n' < /proc/$nautilus_pid/environ | grep '^DBUS_SESSION_BUS_ADDRESS=')
export DBUS_SESSION_BUS_ADDRESS

Code:
export `grep ^DBUS_SESSION_BUS_ADDRESS \
$HOME/.dbus/session-bus/$(dbus-uuidgen --get)-$(echo $DISPLAY \
| sed -e 's/\([^:]*:\)//g' -e 's/\..*$//g')`

I must say that both methods seem very inelegant! And I only know that they work on my Linux desktop which runs Ubuntu with Gnome v.2. Is there a better generic and reliable way to get and set the value of DBUS_SESSION_BUS_ADDRESS from a script called from crontab? If so, then how?

Finally, could some kind soul explain to me why I need set the DBUS_SESSION_BUS_ADDRESS so that the wallpaper will successfully be changed when running the script from crontab, while it is unnecessary when running the same script from the command line?

Many thanks all.
# 2  
Old 10-15-2012
crontab doesn't login as you, so it doesn't get access to X.

Some of those things seem unavoidable, like the uidgen --get. Others, like the echo $DISPLAY | sed 'enormous ugly regex', can probably be made less ugly. And the eval almost certainly isn't necessary.

Without seeing what it's actually doing, of course, what those commands output and its end result, I can't rewrite it.

Last edited by Corona688; 10-15-2012 at 01:47 PM..
# 3  
Old 10-15-2012
Thanks Corona.

Quote:
Originally Posted by Corona688
crontab doesn't login as you, so it doesn't get access to X.
Ok, got it.

Quote:
Originally Posted by Corona688
Some of those things seem unavoidable, like the uidgen --get. Others, like the echo $DISPLAY | sed 'enormous ugly regex', can probably be made less ugly. And the eval almost certainly isn't necessary.

Without seeing what it's actually doing, of course, what those commands output and its end result, I can't rewrite it.
In the absence of any other ways of doing it (anyone?), which would you think was most likely to work with different flavours of Unix/Linux and Gnome v.2 or 3? Or do you think both will work regardless of Unix/Linux and Gnome version?
# 4  
Old 10-15-2012
Extremely difficult to say. dbus is a completely generic thing that can be used in a multitude of ways. Guaranteeing later versions of Gnome would work the same way because they use dbus would be like guaranteeing later versions of Gnome work the same way because they use files...
# 5  
Old 10-15-2012
Ok thanks again for the help.

Anyone else got further info. on this?

Cheers all.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass values to a script called from within another script in shell?

Ceiling Light - The Forgotten Element One of the highest details concerning using an LED ceiling panel essentially offer a fantastic dance floor which definitely makes the customers dance right away.They are a quite low cost method of something like a lighting solution, simple collection up,... (1 Reply)
Discussion started by: harveyclayton
1 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Shell Programming and Scripting

Need output of script on screen and file with correct return status of the called script.

Hi, I am trying to capture logs of the script in the file as well as on the screen. I have used exec and tee command for this. While using exec command I am getting the correct output in the file but, script output is not getting displayed on the screen as it get executed. Below is my sample... (14 Replies)
Discussion started by: Prathmesh
14 Replies

4. Shell Programming and Scripting

Passing variable from called script to the caller script

Hi all, Warm regards! I am in a difficult situation here. I have been trying to create a shell script which calls another shell script inside. Here is a simplified version of the same. Calling Script. #!/bin/ksh # want to run as a different process... (6 Replies)
Discussion started by: LoneRanger
6 Replies

5. Shell Programming and Scripting

Script will keep checking running status of another script and also restart called script at night

I am using blow script :-- #!/bin/bash FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not if then # echo "process found" exit 0; else echo "process not found" exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Discussion started by: ketanraut
1 Replies

6. Shell Programming and Scripting

Expect script called in loop from Bash Script

Having issues with an expect script. I've been scripting bash, python, etc... for a couple years now, but just started to try and use Expect. Trying to create a script that takes in some arguments, and then for now, just runs a pwd command(for testing, final will be command I pass). Here is... (0 Replies)
Discussion started by: cbo0485
0 Replies

7. UNIX for Dummies Questions & Answers

How to retrieve the value of variable in shell script which is called by crontab

There are two files one is shell script (sample.sh) and another is configuration file (sampl_conf.cfg) configuration file contains one variable $FTP_HOME. the value of this variable vaires for user to user. If user is say jadoo then value is /home/jadoo/ftp/, for user1 - /home/user1/ftp. The... (4 Replies)
Discussion started by: jadoo_c2
4 Replies

8. Shell Programming and Scripting

environment variable in shell script called through crontab

Please help me on below.. https://www.unix.com/shell-programming-scripting/141533-retrieve-value-environment-variable-shell-script-called-crontab.html#post302442024 I'm still here. I can still see you! (0 Replies)
Discussion started by: jadoo_c2
0 Replies

9. Shell Programming and Scripting

Retrieve the value of environment variable in shell script which called from crontab

There are two files one is shell script (sample.sh) and another is configuration file (sampl_conf.cfg) configuration file contains one variable $FTP_HOME. the value of this variable vaires for user to user. If user is say jadoo then value is /home/jadoo/ftp/, for user1 - /home/user1/ftp. The... (0 Replies)
Discussion started by: jadoo_c2
0 Replies

10. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies
Login or Register to Ask a Question