tags:

views:

162

answers:

5

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.

+7  A: 

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

Matthew Vines
doh. so easy...
scottm
No worries, happens to us all. Glad I could help.
Matthew Vines
This is waht I'd do. +1.
Triptych
This solution is expedient, but the right solution is to check for a tty/terminal
Michael Donohue
I would prefer to not have the argument, so I've accepted another answer. Thanks, though!
scottm
+6  A: 

You want to check if you're on a terminal or not. See this stack overflow question: http://stackoverflow.com/questions/911168/detect-if-shell-script-is-running-through-a-pipe

Michael Donohue
+2  A: 

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
+9  A: 

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
+4  A: 

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
will this confuse processes run by applications other than cron with cron jobs?
Evgeny
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