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

前端 未结 3 1904
北海茫月
北海茫月 2020-12-21 16:27

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?

EDI

3条回答
  •  遥遥无期
    2020-12-21 16:57

    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--
    

提交回复
热议问题