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
?
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:
And, near the beginning of the
cron
job:Read
man xhost
.Try adding this line to your script (obviously before the call to
gvfs-set-attribute
):It picks up the
DBUS_SESSION_BUS_ADDRESS
environment variable from your existing gnome-session. This makes two assumptions though: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 topgrep
. For example:If you are not using gnome - then I think this will work with other desktop environments by replacing
gnome-session
with another value - such aslxsession
for LXDE.