How to tell when a script is executed by cron ?

Simon Dobner sdobner at scu.edu.au
Wed Feb 5 00:20:33 EST 2003


On Wed, 05 Feb 2003 11:41:45 +1100, Simon Dobner wrote:

> I want to know how to tell if a script is being executed in the background
> by cron, as opposed to running in the foreground from the command prompt.

OK, I had a bit of a play and came up with this

import sys
import termios

try:
    termios.tcgetattr(sys.stdout)
    #this IS a terminal, therfore we are running interactivly
    # Do nothing
except:
    #this is NOT a terminal so
    logfile = open('/tmp/logfile', 'w')
    sys.stdout = logfile

print "Some Output"    

if sys.stdout != sys.__stdout__:
    sys.stdout = sys.__stdout__
    logfile.close()



I'd appreciate feedback on this - is it a good idea or have I missed
something ?

Regards

SD




More information about the Python-list mailing list