本文介绍了我如何确定是否从crontab执行python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何确定是否从crontab执行python脚本?
I would like to know how can I determine if a python script is executed from crontab?
我不想要一个解决方案,需要添加一个参数,因为
I don't want a solution that will require adding a parameter because I want to be able to detect this even from an imported module (not the main script).
推荐答案
不是您要求的内容,但也许你想要的是 os.isatty(sys.stdout.fileno())
,它告诉 stdout
(大致而言)连接到终端。如果将输出传递到文件或另一个进程,或者从cron运行进程,则它将为false。
Not quite what you asked, but maybe what you want is os.isatty(sys.stdout.fileno())
, which tells if stdout
is connected to (roughly speaking) a terminal. It will be false if you pipe the output to a file or another process, or if the process is run from cron.
这篇关于我如何确定是否从crontab执行python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!