Mysterious EOFError

R

Rex Eastbourne

Hi,

I'm executing a python script as a cron job. When I run it myself from
the command line it works, but when cron executes it I get an EOFError:

File "/home/rex/cronscript.py", line 6, in ?
level = int(raw_input("hello?"))
EOFError: EOF when reading a line

It's not the last line of the file.

When I run it as root from the command line I don't get any errors.

Any ideas?
 
S

Steven D'Aprano

Hi,

I'm executing a python script as a cron job. When I run it myself from
the command line it works, but when cron executes it I get an EOFError:

File "/home/rex/cronscript.py", line 6, in ?
level = int(raw_input("hello?"))
EOFError: EOF when reading a line


Because raw_input is waiting for input from the user. I assume that when
cron runs a Python script, and there is no actual human user to enter a
string in response to raw_input, it raises a EOF error.

Written in the docs:

http://docs.python.org/lib/module-exceptions.html


exception EOFError
Raised when one of the built-in functions (input() or raw_input())
hits an end-of-file condition (EOF) without reading any data. (N.B.:
the read() and readline() methods of file objects return an empty
string when they hit EOF.)
 
R

Rex Eastbourne

Thanks. Do you know of a solution to this? I tried the following, which
I found on this newsgroup:

#============

lines = open(sys.argv[1]).readlines()

#============

sys.stdin = open('/dev/tty')
a = raw_input('Prompt: ')

#============

sys.stdin = os.fdopen(3)
a = raw_input('Prompt: ')

#============

What I want to do is execute a scheduled task that prompts me for
something and allows me to enter it and have it stored somewhere. I'm
on Ubuntu Linux.

Thanks in advance!
 
D

Dennis Lee Bieber

lines = open(sys.argv[1]).readlines()
This appears to rely on the program being given a filename on the
command line which it will then open and read for data...
#============

sys.stdin = open('/dev/tty')
a = raw_input('Prompt: ')
This one presumes the program can open a TTY connection -- but if
you are running a graphical environment, the TTY is likely one of the
hidden consoles (I think Mandrake allowed swapping by using some
combination of <f1> or so... <f8> being the graphical window, <f9> being
system log output, and <f1..6> being six independent console logins). Or
worse -- TTY might be the serial port...
#============

sys.stdin = os.fdopen(3)
a = raw_input('Prompt: ')

Is that attempting to do a read from stderr? Same problem -- it is
looking for a console connection which doesn't exist for cron jobs.
#============

What I want to do is execute a scheduled task that prompts me for
something and allows me to enter it and have it stored somewhere. I'm
on Ubuntu Linux.
Have you considered trying to code the task to open a Tkinter (or
other) window with a message box/prompt? Of course, that probably
implies you have to be running in an X environment...

So far as I know, cron jobs -- by nature -- are designed to NOT be
interactive.
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top