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
  • 37,856
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.