By Nate Mara


2014-12-17 20:48:50 8 Comments

I wrote a python script to monitor the statuses of some network resources, an infinite pinger if you will. It pings the same 3 nodes forever until it receives a keyboard interrupt. I tried using tee to redirect the output of the program to a file, but it does not work:

λ sudo ./pingster.py

15:43:33        node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:35        node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:36        node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:37        node1 SUCESS | node2 SUCESS | node3 SUCESS
15:43:38        node1 SUCESS | node2 SUCESS | node3 SUCESS
^CTraceback (most recent call last):
  File "./pingster.py", line 42, in <module>
    main()
  File "./pingster.py", line 39, in main
    sleep(1)
KeyboardInterrupt

λ sudo ./pingster.py | tee ping.log
# wait a few seconds
^CTraceback (most recent call last):
  File "./pingster.py", line 42, in <module>
    main()
  File "./pingster.py", line 39, in main
    sleep(1)
KeyboardInterrupt

λ file ping.log
ping.log: empty 

I am using colorama for my output, I thought that perhaps could be causing the issue, but I tried printing something before I even imported colorama, and the file is still empty. What am I doing wrong here?

Edit: Here is the python file I'm using

#!/home/nate/py-env/ping/bin/python

from __future__ import print_function
from datetime import datetime
from collections import OrderedDict
from time import sleep

import ping
import colorama


def main():
    d = {
        'node1': '10.0.0.51',
        'node2': '10.0.0.50',
        'node3': '10.0.0.52',
    }
    addresses = OrderedDict(sorted(d.items(), key=lambda t: t[0]))

    colorama.init()
    while True:
        status = []
        time = datetime.now().time().strftime('%H:%M:%S')
        print(time, end='\t')
        for location, ip_address in addresses.items():
            loss, max_time, avg_time = ping.quiet_ping(ip_address, timeout=0.5)
            if loss < 50:
                status.append('{0} SUCESS'.format(location))
            else:
                status.append(
                    '{}{} FAIL{}'.format(
                        colorama.Fore.RED,
                        location,
                        colorama.Fore.RESET,
                    )
                )
        print(' | '.join(status))
        sleep(1)

if __name__ == '__main__':
    main()

1 comments

@that other guy 2014-12-17 21:10:25

Here's a simpler way of reproducing your issue:

$ cat foo.py
from time import sleep
while True: 
  sleep(2)
  print "hello"

$ python foo.py
hello
hello    
(...)

$ python foo.py | tee log
(no output)

This happens because python buffers stdout when it's not a terminal. The easiest way to unbuffer it is to use python -u:

$ python -u foo.py | tee log
hello
hello
(...)

You can also set the shebang to #!/usr/bin/python -u (this does not work with env).

@dimo414 2016-07-11 22:06:17

If you can't modify the python invocation you can set the PYTHONUNBUFFERED environment variable to a non-empty string and this will have the same effect. man python describes the behavior in more detail

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] Does Python have a string &#39;contains&#39; substring method?

29 Answered Questions

[SOLVED] How to change the output color of echo in Linux

23 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

21 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 3704565 View
  • 3474 Score
  • 21 Answer
  • Tags:   python directory

29 Answered Questions

[SOLVED] What does if __name__ == "__main__": do?

38 Answered Questions

[SOLVED] What does the "yield" keyword do?

19 Answered Questions

[SOLVED] How do I tell if a regular file does not exist in Bash?

  • 2009-03-12 14:48:43
  • Bill the Lizard
  • 2406350 View
  • 3056 Score
  • 19 Answer
  • Tags:   bash file-io scripting

37 Answered Questions

[SOLVED] How do I check whether a file exists without exceptions?

14 Answered Questions

[SOLVED] How do I set a variable to the output of a command in Bash?

15 Answered Questions

[SOLVED] In the shell, what does " 2>&1 " mean?

  • 2009-05-03 22:57:00
  • Tristan Havelick
  • 1025827 View
  • 2116 Score
  • 15 Answer
  • Tags:   bash shell unix redirect

Sponsored Content