1

I am trying to run a simple python script with cron and I cannot for the life of me get it to work. I am running Ubuntu 18.04.

I know there are many questions (and answers) on this. I have looked at a number of them and I still cannot get it to work.

My python script is:

#!/usr/bin/env python

from tkinter import messagebox
messagebox.showinfo('MessageBox','Hello world!')

I have followed Unable to Run Python Script In Cron and changed the permissions as follows:

chmod +x /home/brb/bugs/projects/cron/hello.py

I can successfully run this from the command line as follows:

brb@bishop:~$ /home/brb/bugs/projects/cron/hello.py 

Inside crontab I have tried to execute the script in 2 different ways as follows (every 2 or 3 minutes to try and pin down which one works...)

*/2 * * * * /home/brb/bugs/projects/cron/hello.py
*/3 * * * * /usr/bin/env python /home/brb/bugs/projects/cron/hello.py

Nothing happens.

I followed http://www.jessicayung.com/automate-running-a-script-using-crontab/ and tried to inspect the error file as follows:

cd ../..
cd var/mail 
nano $brb

and I observe a message in nano that says

[ Directory '.' is not writable ]

I am not sure which directory '.' is, perhaps it is var/mail. In any case, not sure how to make it writable. I will google chmod some more to get this writable I guess, but that still doesn't solve why cron is not working when it looks like all the examples I've seen...

EDIT:

I followed How to run an X program from outside the X session (e.g. from the console or SSH) as per the comment from triplee and, not really knowing what I am doing, I attempted to change my display permissions as follows:

brb@bishop:~$ export XAUTHORITY=/home/brb/.Xauthority
brb@bishop:~$ export DISPLAY=':0'

My cron job is still not work...

5
  • Possible duplicate of How to run an X program from outside the X session (e.g. from the console or SSH) Commented Feb 9, 2019 at 10:14
  • You are not supposed to have write access to /var/mail and you are not supposed to edit your mail spool file. $brb is probably an empty string anyway. You can less /var/mail/brb to read your mailbox, though using a proper mail program is probably more pleasant. Commented Feb 9, 2019 at 10:16
  • Thanks. I read stackoverflow.com/questions/1584411/… and set the two environment variables as per the accept answer and it still does not work. I don't believe it is a duplicate question... Commented Feb 9, 2019 at 10:30
  • 1
    Things you export at the prompt are not available to cron. See the Stack Overflow crontab tag info page for troubleshooting guidelines. This is almost certainly a duplicate. Commented Feb 9, 2019 at 15:06
  • Thanks, I will read this. Appreciate the help. Commented Feb 10, 2019 at 4:50

1 Answer 1

3

Your problem is twofold:

  • cronjob has does not know where it could be displaying a graphical app (which X server or DISPLAY to use.

    This can be addressed by making sure DISPLAY is set in the environment of you script. Run echo $DISPLAY in a terminal running in your X session. :0 is a very likely it.

  • Even if it knows the correct DISPLAY, it does (should) not have access to it. If you are running the cronjob under the same user as the one running your target X session. If it is a different user, and it has access to your user's home, you could reuse it's authority file by setting XAUTHORITY to point to it.

    You can also export necessary information from your running session (renewed with each session), type this into a terminal there: xauth list $DISPLAY and add it for your cron running user: xauth add displayname protocolname hexkey (using those three values as returned by xauth list.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I did run echo $DISPLAY and got :0 as you predicted. When I run xauth list $DISPLAY I get.... bishop/unix: MIT-MAGIC-COOKIE-1 cd4b7cc327d6387c3535465f23f1b50b #ffff#626973686f70#: MIT-MAGIC-COOKIE-1 cd4b7cc327d6387c3535465f23f1b50b Should I add this to the crontab script? I am not sure about the last part of your answer... I will also read the link in the comment about and see if this helps me understand, but if you are able to say what I put where, they would help me close of the question. Appreciate your help.
In that case, you should run/add: xauth add bishop/unix:0 MIT-MAGIC-COOKIE-1 cd4b7cc327d6387c3535465f23f1b50b Beware though the actual cookie value changes with each X session (re)start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.