2

Hey I am running into an issue when trying to run a cron job with a python script from ubuntu. This is what I have done:

1.) Wrote a simple tkinter app: source for the code is from this url - http://www.ittc.ku.edu/~niehaus/classes/448-s04/448-standard/simple_gui_examples/sample.py

#!/usr/bin/python
from Tkinter import *
class App:
    def __init__(self,parent):
        f = Frame(parent)
        f.pack(padx=15,pady=15)
        self.entry = Entry(f,text="enter your choice")
        self.entry.pack(side= TOP,padx=10,pady=12)
        self.button = Button(f, text="print",command=self.print_this)
        self.button.pack(side=BOTTOM,padx=10,pady=10)
        self.exit = Button(f, text="exit", command=f.quit)
        self.exit.pack(side=BOTTOM,padx=10,pady=10)

    def print_this(self):
        print "this is to be printed"

root  = Tk()
root.title('Tkwidgets application')
app = App(root)
root.mainloop()

2.) changed the script to become executable:

chmod 777 sample.py

3.) Added the script to my cronjob to be run every minute for testing purposes. I opened crontab -e and added the following to my file:

 * * * * * /home/bbc/workspace/python/tkinter/sample.py 

4.) Disclaimer: I did not add any additional environment variables for tkinter nor did I change my cronjob script at /etc/init.d/cron

5.) I was tracking the cron job by doing a tail -f /var/log/syslog

$ tail -f /var/log/syslog
Jul  7 18:33:01 bbc CRON[11346]: (bbc) CMD (/home/bbc/workspace/python/tkinter/sample.py)
Jul  7 18:33:01 bbc CRON[11343]: (CRON) error (grandchild #11344 failed with exit status 1)
Jul  7 18:33:01 bbc CRON[11343]: (CRON) info (No MTA installed, discarding output)
Jul  7 18:33:01 bbc CRON[11342]: (CRON) error (grandchild #11346 failed with exit status 1)
Jul  7 18:33:01 bbc CRON[11342]: (CRON) info (No MTA installed, discarding output)

Any help on debugging this issue will be most appreciated...

Dhawan Gayash
  • 463
  • 1
  • 4
  • 17
  • Could you be more specific about what you expected to happen? I'm not sure what issue you're asking for help with. – James Polley Jul 08 '13 at 01:41
  • I am trying the gui portion to prompt my screen. I have changed the print_this method to get the input from text box and write to a file. My concern is my script throws an -- "error (grandchild #11475 failed with exit status 1)" – Dhawan Gayash Jul 08 '13 at 01:47
  • When you say "Added the script to my cronjob" - which cronjob? Something under /etc/cron.d? Your personal crontab from `crontab -e`? – James Polley Jul 08 '13 at 01:49
  • I edited the src with correct indentations. I did not add any new scripts under /etc/cron.d I used crontab -e and added the line provided in my description at step 3. – Dhawan Gayash Jul 08 '13 at 01:55
  • 1
    @James Hey I finally got it to work. You pointed me to the right direction by asking which display to use. After some googling found this https://help.ubuntu.com/community/CronHowto under the GUI Applications they ask to add the cron with env DISPLAY=:0.0 gui_appname this worked. Thanks – Dhawan Gayash Jul 08 '13 at 02:06
  • Excellent :) You should at that note as an answer so that you can accept it – James Polley Jul 08 '13 at 02:23

3 Answers3

3

I'm not sure what you expect to happen here. The cronjob won't have access to a display where it can display the GUI, so the button will never be displayed, so print_this will never be run

FWIW, when I tried to run your code I got an error:

  File "./t.py", line 4
    def __init__(self,parent):
      ^
IndentationError: expected an indented block

Not sure if that's just caused by copy/paste into the page or if it's a real problem with your code.

James Polley
  • 7,977
  • 2
  • 29
  • 33
  • I am trying the gui portion to prompt my screen. I have changed the print_this method to get the input from text box and write to a file. My concern is my script throws an error (grandchild #11475 failed with exit status 1) – Dhawan Gayash Jul 08 '13 at 01:46
  • My sample code here is a cleaned out version of this url code - http://www.ittc.ku.edu/~niehaus/classes/448-s04/448-standard/simple_gui_examples/sample.py – Dhawan Gayash Jul 08 '13 at 01:50
2

In linux mint 17 I had to do the following:

Make the scripts executable
~$chmod +x script.py

You have to enable X ACL for localhost to connect to for GUI applications to work
~$ xhost +local:

Add the following line in the crontab "env DISPLAY=:0.0"
* * * * * env DISPLAY=:0.0 /usr/bin/python /your-script-somewhere.py

And one more line to crontab ">/dev/null 2>&1"
* * * * * env DISPLAY=:0.0 /usr/bin/python /your-script-somewhere.py >/dev/null 2>&1

you can check errors in /var/log/syslog file
~$ tail -20 /var/log/syslog

more info:
https://help.ubuntu.com/community/CronHowto

Iwashere
  • 21
  • 2
1

I use crontab to run a bash file

30 12 * * 1,2,3,4,5 /home/edward/SSF/SW/EODWD.sh

in termanal -- use crontab -e

the bash file executes as many other programs as you wish

/home/edward/SSF/SW/EODWD.py >> /home/edward/Desktop/eodmail.log wait

this example also sends all print statements in EODWD.py to a log file automatically

the wait statement forces competion before taking the next command

this works ONLY IF both files ( *.py & *.sh ) are made executable

edward
  • 11
  • 1