Why can I send input to i3bar with bash script, but not with python?
asked 2015-07-13 13:28:38 +0000
This post is a wiki. Anyone with karma >100 is welcome to improve it.
As a test, I added this to my .i3/config:
bar {
status_command i3status | ~/.i3/status.sh
}
with the script:
#!/bin/bash
echo '{ "version": 1 }'
echo '['
echo '[]'
while :;
do
echo ",[{\"name\":\"time\",\"full_text\":\"test\"}]"
sleep 1
done
I see "test" on the i3bar. Nice. However, I want to use python for the real script, so I tried:
bar {
status_command i3status | ~/.i3/status.py
}
with:
#!/usr/bin/env python
import time
print '{ "version": 1 }'
print '['
print '[]'
while True:
print ',[{"name":"time","full_text":"test"}]'
time.sleep(1)
but... no joy. Nothing on the i3bar. Running the scripts in terminal, the output looks the same. Even output to file and examined in hex, and it was the same. Is it some sort of problem with buffering? Even if I wait, nothing shows up on the bar. Tried running with "python ~/.i3/status.py", still nothing. Am I missing something obvious? (That does happen to me a lot.) Thanks in advance!
Yes, it's buffering. Try using "stdbuf -o 0 -e 0 python2.7 ~/.i3/status.py" and you can see it works.
Thanks! Looks like you just beat me to it. Can't upvote, but props for the quick reply. There's a good discussion on the issue here (explains why it all looked fine in terminal): http://stackoverflow.com/questions/15731231/python-script-prints-output-of-os-system-before-print
To be fair, stdbuf only meant to prove the theory, not to be a solution, so it wasn't an answer. Using "-u" is the right solution :)