我有一个脚本,它通过写入同一个控制台行来显示节目的上传进度。当脚本从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%]

我只想知道我是否可以从脚本内部判断它是否是从cron调用的,如果是,我不会显示这个输出。

最佳答案

见下面的代码。用您想要显示的内容替换我的打印报表。

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

10-06 05:31