2

I'm creating a script to manage my icons and I'm using the command gvfs-set-attribute in order to do that. The script works fine when I run it with the terminal but it fails to work when I use cron. Here is a simplified version of my problem:

#!/bin/bash

PATH=/home/myUser/bin:/home/myUser/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/home/myUser/.local/share/gvfs-metadata

gvfs-set-attribute -t string /home/myUser/myFolder metadata::custom-icon file:///home/myUser/myImage.png

I've used crontab -e and settled my script to run every minute * * * * * /home/myUser/script.bash. The problem is that this script doesn't work at all when I run it with cron.

I've already written the PATH on my script as described in this other issue and the output of the command whereis gvfs-set-attribute is:

gvfs-set-attribute: /usr/bin/gvfs-set-attribute /usr/share/man/man1/gvfs-set-attribute.1.gz

So, I'm assuming there's nothing wrong with the PATH here. I've also tried to run my script directly with the absolute path: /usr/bin/gvfs-set-attribute. But any of those things worked... Does anyone have any idea of what's happening and why I can't use the command gvfs-set-attribute with cron?

2 Answers2

1

cron jobs do not run under the X window system, and do not normally access GUI objects.

However, you can cheat.

In a terminal, running under the GUI:

xhost +localhost

echo "export DISPLAY=\"$DISPLAY\"" >$HOME/.display

And, near the beginning of the cron job:

source $HOME/.display

Read man xhost.

waltinator
  • 36,399
  • Thanks for the reply. This approach works fine if I try to run any GUI application with cron. But it still didn't solve my problem... The gvfs-set-attribute is actually a command line tool. – Rafael Muynarsk May 23 '18 at 05:33
1

Try adding this line to your script (obviously before the call to gvfs-set-attribute):

export $(cat /proc/$(pgrep gnome-session)/environ | grep -z ^DBUS_SESSION_BUS_ADDRESS)

It picks up the DBUS_SESSION_BUS_ADDRESS environment variable from your existing gnome-session. This makes two assumptions though:

  1. You are running a gnome-session; and
  2. There is only one gnome-session

If there are multiple gnome-sessions (i.e. one each for multiple users) you can change this line to work for a specific user by adding the -u option to pgrep. For example:

export $(cat /proc/$(pgrep gnome-session -u myUser)/environ | grep -z ^DBUS_SESSION_BUS_ADDRESS)

If you are not using gnome - then I think this will work with other desktop environments by replacing gnome-session with another value - such as lxsession for LXDE.

Brendan McGrath
  • 374
  • 2
  • 4
  • That worked!! Thanks... So the problem was that gvfs-set-attribute relies on the environment variable DBUS_SESSION_BUS_ADDRESS. But how did you figure that out? Did you already know that gvfs uses this environment variable or there's a way to check what variables are being used by a specific command? – Rafael Muynarsk May 23 '18 at 15:47
  • To be honest I got lucky with a couple of Google searches:
    • https://translate.google.com.au/translate?hl=en&sl=fr&u=http://ubuntu-xp.blogspot.com/2011/02/gvfs-set-attribute-et-cron.html&prev=search
    • https://unix.stackexchange.com/questions/250276/access-to-users-session-d-bus-from-their-cron-commands
    – Brendan McGrath May 24 '18 at 04:09
  • After upgrading my system to Ubuntu 18.04 this approach doesn't work anymore (it was 16.04 when I asked this question). I've realized that the file environ is owned by "gdm" and it just has reading permissions for its owner (like if gdm executed "chmod 400 environ"). The consequence is that the cat command doesn't have enough privileges to read the environ file and I trying to add my user to gdm's group doesn't work. Do you know a workaround to solve this problem? – Rafael Muynarsk Jun 07 '18 at 06:02
  • Well, as a temporary solution I've added the line "export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus" directly on my script. That works well but I don't really know the consequences of using a fixed value there and if this path changes under some circumstances. But as a functional solution that's fine... – Rafael Muynarsk Jun 07 '18 at 08:53