Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check to see if a Python script was started interactively?

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.

like image 628
Geo Avatar asked Nov 16 '25 16:11

Geo


2 Answers

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.

like image 149
Ned Batchelder Avatar answered Nov 18 '25 09:11

Ned Batchelder


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()
like image 37
cobbal Avatar answered Nov 18 '25 08:11

cobbal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!