I am using ios-webkit-proxy-debug remote server which usually shuts down or just disconnects. I want to restart server if last line of out contains "Disconnected" or command is not running at all.
1 Answer
The problem may be that the output is buffered. You may have luck with the util "stdbuf" to disable the buffer. (another tool is "unbuffer") You can fully disable all buffers with:
stdbuf -i0 -o0 -e0 [command] # 0 is unbuffered and L is line-buffered
Your command might look like this:
stdbuf -oL -eL ios_webkit_debug_proxy |& tee -a proxy.log
tail -f -n0 proxy.log | grep --line-buffered "Disconnected" | while read line ; do [restart server] ; done
I tested it with this:
# This is one terminal
cd /tmp
echo > log
# This in another terminal
cd /tmp
tail -f -n0 log | grep --line-buffered "disconnect" | while read line ; do echo "found disconnect" ; done
# Then, in the first terminal
echo "test" >> log # second terminal does nothing
echo "disconnect" >> log # second terminal echos "found disconnect"
The tail -n0 is because if the tail reads a disconnect in a log file that already exists, it will restart the server as soon as you run that command.
EDIT: stdbuf is overridden by tee (see man tee). You may have more luck in a different format but some stuff to play around with:
stdbuf -oL -eL ios_webkit_debug_proxy 2>&1 >> proxy.log
# or
unbuffer ios_webkit_debug_proxy |& tee -a proxy.log | grep --line-buffered "Disconnected" | while read line ; do [restart server] ; done
6 Comments
mathewguest
Sorry friend! stsdbuf was a typo-- it was meant to be stdbuf. These binaries have to be installed. What operating system do you use? On Arch, stdbuf is in core/coreutils and unbuffer is is in extra/expect. Look for packages named something similar to "GNU Coreutils" and "expect".
Kirill
I am using OSX. The last one.
mathewguest
I don't know too much about OSX. Some googling suggests that you can install packages with this software called Brew. I've never used this though. Link: brew.sh Command: brew install coreutils
Kirill
No problem! I suppose, there is no such package on OSX. I found another approach to my problem without getting logs, though. Thank you for help anyway!
Shyamala Selvaganapathy
@KirillZhukov Can you please share the approach you followed? I am running into a similar issue.
|
ios_webkit_debug_proxy 2>&1 | tee proxy.log, but log is not getting updated (empty), until I kill server process...ios_webkit_debug_proxy >> proxy.log 2>&1and thentail -F proxy.log | grep "Disconnected"