1

I wrote a bash script to display the current xkcd comic in my web browser (using the RXKCD package in R). I want to schedule this so that the current xkcd comic displays every morning at 8am when I get into work. However, crontab doesn't seem to want to cooperate with me. I've found lots of material online where crontab fails, but still can't seem to figure out what's wrong with my situation.

Here is the bash script:

#! /bin/bash
R CMD BATCH todaysXKCD.R

and here is 'todaysXKCD.R'

library(RXKCD)
getXKCD(html=T, display=F)

I've set up a crontab using crontab -e, and that looks like this:

SHELL=/bin/bash
PATH=/home/pfrater/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
* * * * * root /bin/bash /home/pfrater/bashScripts/xkcd/todaysXKCD.sh

The script works perfectly when I call it from the command line, but not at all with cron. What am I doing wrong? Do I need to add the path for my bashscript to the PATH in the crontab?

  • Possible duplicate of How to start a GUI application from cron? – muru Apr 7 '16 at 15:33
  • If it opens the browser, you need to have DISPLAY set. – muru Apr 7 '16 at 15:34
  • Thanks, muru. I think this is it. When I set the crontab file to * * * * * DISPLAY=:0 firefox /home/pfrater/bashScripts/xkcd/todaysXKCD.sh it now opens firefox, but just shows the script file as plain text in firefox. I'll have to play around with it to get it to work. – PaulFrater Apr 7 '16 at 15:40
  • 1
    Okay, perfect. Instead of using the script I just put 0 8 * * 1-5 DISPLAY:0 firefox xkcd.com. Works great. Thanks. – PaulFrater Apr 7 '16 at 15:43
  • How about a DISPLAY=:0 /home/pfrater/bashScripts/xkcd/todaysXKCD.sh? – muru Apr 7 '16 at 15:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.