如何确定是否从 crontab 执行了 python 脚本? [英] How can I determine if a python script is executed from crontab?

查看:18
本文介绍了如何确定是否从 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 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆