简体   繁体   English

如何知道我的脚本是从cronjob还是从命令行运行?

[英]How can I tell if my script is being run from a cronjob or from the command line?

I have a script and it's display show's upload progress by writing to the same console line. 我有一个脚本,它通过写入同一控制台行来显示节目的上传进度。 When the script is run from a cron job, rather than writing to a single line, I get many lines: 当脚本是从cron作业运行的,而不是写到一行时,我得到很多行:

***   E0710091001.DAT  ***   [0.67%]
***   E0710091001.DAT  ***   [1.33%]
***   E0710091001.DAT  ***   [2.00%]
***   E0710091001.DAT  ***   [2.66%]
***   E0710091001.DAT  ***   [3.33%]
***   E0710091001.DAT  ***   [3.99%]
***   E0710091001.DAT  ***   [4.66%]
***   E0710091001.DAT  ***   [5.32%]
***   E0710091001.DAT  ***   [5.99%]
***   E0710091001.DAT  ***   [6.65%]
***   E0710091001.DAT  ***   [7.32%]
***   E0710091001.DAT  ***   [7.98%]
***   E0710091001.DAT  ***   [8.65%]
***   E0710091001.DAT  ***   [9.32%]
***   E0710091001.DAT  ***   [9.98%]
***   E0710091001.DAT  ***   [10.65%]
***   E0710091001.DAT  ***   [11.31%]
***   E0710091001.DAT  ***   [11.98%]
***   E0710091001.DAT  ***   [12.64%]
***   E0710091001.DAT  ***   [13.31%]
***   E0710091001.DAT  ***   [13.97%]
***   E0710091001.DAT  ***   [14.64%]
***   E0710091001.DAT  ***   [15.30%]
***   E0710091001.DAT  ***   [15.97%]
***   E0710091001.DAT  ***   [16.63%]
***   E0710091001.DAT  ***   [17.30%]
***   E0710091001.DAT  ***   [17.97%]
***   E0710091001.DAT  ***   [18.63%]

I just want to know if I can tell from inside the script if it's being called from cron, and if so, I won't display this output. 我只想知道是否可以从脚本内部判断是否从cron调用了它,如果可以,则不会显示此输出。

you could create a flag. 您可以创建一个标志。 Possibly undocumented that your cron job would pass to the utility to structure the output. 可能没有记录,您的cron作业将传递给实用程序以构造输出。

我将检查sys.stderr.isatty() -这样一来,无论何时用户无法立即感知到stderr,都可以避免无用的“装饰”输出。

See code below. 请参见下面的代码。 Replace my print statements with what you want to show. 用您要显示的内容替换我的打印报表。

import sys
if sys.stdout.isatty():
    print "Running from command line"
else:
    print "Running from cron"

You want to check if you're on a terminal or not. 您想检查您是否在终端上。 See this stack overflow question: How to detect if my shell script is running through a pipe? 看到这个堆栈溢出问题: 如何检测我的shell脚本是否正在通过管道运行?

一种简单的方法是让脚本采用一个参数,该参数表示抑制该输出,并在您从cron调用它时提供该参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何运行我的 Python 脚本? 为什么命令行告诉我“没有这样的文件或目录”? - How do I run my Python script? Why does the command line tell me "no such file or directory"? 如何让我的python(2.5版)脚本在文件夹而不是命令行中运行jar文件? - How can I get my python (version 2.5) script to run a jar file inside a folder instead of from command line? 我可以从命令行运行python脚本的一部分吗? - Can I run a portion of a python script from command line? 如何安装脚本以从命令行在任何地方运行? - How do I install a script to run anywhere from the command line? 从我的Python脚本内部,我想安排另一个Python脚本或命令行脚本的延迟运行 - From inside my Python script, I want to schedule a deferred run of another Python script or a command line script Python-如何在IDE中使用不同的命令行参数调用脚本? - Python - How can i call the script with different command line arguments from within my IDE? 如何从命令行或其他脚本运行 python 脚本 - How to run python script from command line or from another script 如何从命令行获取输出到脚本中的条件? - How can I get output from command line into condition in script? 我如何使用 python 脚本中的命令行 - How can i use command line from python script 如何从Python脚本启动GIMP命令行? - How can i launch the GIMP command line from a Python script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM