3

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:

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

scottm
  • 27,829
  • 22
  • 107
  • 159

5 Answers5

10

you could create a flag. Possibly undocumented that your cron job would pass to the utility to structure the output.

Matthew Vines
  • 27,253
  • 7
  • 76
  • 97
  • No worries, happens to us all. Glad I could help. – Matthew Vines Jul 10 '09 at 15:25
  • 1
    This solution is expedient, but the right solution is to check for a tty/terminal – Michael Donohue Jul 10 '09 at 15:31
  • I would prefer to not have the argument, so I've accepted another answer. Thanks, though! – scottm Jul 10 '09 at 15:39
  • Document the flag. What if in the future you want this functionality adapted for another method of the script being called? A person could easily see the "--iscron Format the output safely for logging when called by cron" and just move on. – bgStack15 Mar 16 '17 at 17:30
9

I'd check sys.stderr.isatty() -- this way you avoid useless "decoration" output to stderr whenever it wouldn't be immediately perceptible by the user anyway.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
8

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"
Mark Roddy
  • 27,122
  • 19
  • 67
  • 71
  • will this confuse processes run by applications other than cron with cron jobs? – Evgeny Jul 10 '09 at 16:49
  • Technically what this actually does is check to see if stdout is attached to a terminal or not. Redirected to a file for instance, which is what cron will do. Given that the OP's question was about determining what to print, I guessed that it is a safe assumption that printing behavior when redirecting to a file is the same regardless of whether it is initiated by cron or not. – Mark Roddy Jul 10 '09 at 17:07
6

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?

Community
  • 1
  • 1
Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
2

An easy way would be to have the script take an argument that means to suppress that output, and supply that argument when you call it from cron.

aem
  • 3,896
  • 24
  • 20