0

I'm trying to create my first cron job. I'm new to bash scripting too, although I do know some python. I am puzzled by the following:

Here is my cronjob file created with crontab -e:

*/1 * * * * /home/darren/.bash_scripts/urxvt_colors.sh

Contents of urxvt_colors.sh:

#!/bin/bash

python  ~/.Py_Scripts/xr_random_colors.py
xrdb ~/.Xresources

Here is what baffles me. So the python part of the cron job works python ~/.Py_Scripts/xr_random_colors.py is executed every minute. This python script changes the color scheme in my ~/.Xresources file. I confirmed this actually happens by checking every minute. But xrdb ~/.Xresources does not update the file.

Running which python shows /usr/bin/python and which xrdb shows /usr/bin/xrdb. So since they are both executed from /usr/bin, how come only the python script executes?

Also if I run ./urxvt_colors.sh script manually from my terminal then it works as expected, the python script runs and so does xrdb ~/.Xresources

What's happening here?

  • 4
    Your cron job does not have the $DISPLAY and possibly $XAUTHORITY environment variables. See https://unix.stackexchange.com/questions/10121/open-a-window-on-a-remote-x-display-why-cannot-open-display – derobert Jun 22 '17 at 14:51
  • Try using a full path in place of ~/ – jc__ Jun 22 '17 at 15:16

1 Answers1

1

Try to change your script like this

#!/bin/bash

python  ~/.Py_Scripts/xr_random_colors.py && xrdb ~/.Xresources

and i recommend you to use full path to files.

PS maybe you need to define DISPLAY var while exec script

*/1 * * * * DISPLAY=:0 /home/darren/.bash_scripts/urxvt_colors.sh
  • Thanks. Adding DISPLAY=:0 to the cronjob got things working. Reading the "This question already has an answer" link above explained to me why :) – Darren Haynes Jun 22 '17 at 22:28