I have a Python script that normally runs out of cron. Sometimes, I want to run it myself in a (Unix) shell, and if so, have it write its output to the terminal instead of writing to a log file.
What is the pythonic way of determining if a script is running out of cron or in an interactive shell (I mean bash, ksh, etc. not the python shell)?
I could check for the existence of the TERM environment variable perhaps? That makes sense but seems deceptively simple...
Could os.isatty somehow be used?
I'm using Python 2.6 if it makes a difference. Thanks!
os.isattyworks fine. Just pass itos.isatty(sys.stdout.fileno()). Or, more simply, use theisattymethod onstdoutitself.