I'd like for a script of mine to have 2 behaviours, one when started as a scheduled task, and another if started manually. How could I test for interactiveness?
EDIT: this could either be a cron job, or started by a windows batch file, through the scheduled tasks.
-
If you want to know if you're reading from a terminal (not clear if that is enough of a distinction, please clarify) you can use
sys.stdin.isatty()
-
You should simply add a command-line switch in the scheduled task, and check for it in your script, modifying the behavior as appropriate. Explicit is better than implicit.
One benefit to this design: you'll be able to test both behaviors, regardless of how you actually invoked the script.
scvalex : Agreed. Run the script normally with awesomeApp and as a batch job with awesomeApp --batch. -
I'd just add a command line switch when you're calling it with cron:
python yourscript.py -scheduled
then in your program
import sys if "-scheduled" in sys.argv: #--non-interactive code-- else: #--interactive code--
Gregg Lind : at least use the conventional "--scheduled"
0 comments:
Post a Comment