Tee does not show output or write to file

后端 未结 1 992
攒了一身酷
攒了一身酷 2020-12-14 18:16

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 int

相关标签:
1条回答
  • 2020-12-14 18:49

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

    0 讨论(0)
提交回复
热议问题