2

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.

6
  • Is there a log file or something that you can look at to check? In bash you can run something like: tail -F logfile.log | grep "Disconnected" Commented Nov 6, 2014 at 19:15
  • Yeah, I can add to the log or txt file. There is a way to capture log, add to file and displayed in console at the same time? Commented Nov 6, 2014 at 19:20
  • If the output of your server is going to the log file, you can just tail the file and grep for "Disconnected". Commented Nov 6, 2014 at 19:24
  • Well, I am using this command to run server, display log in the screen and capture it: ios_webkit_debug_proxy 2>&1 | tee proxy.log, but log is not getting updated (empty), until I kill server process... Commented Nov 6, 2014 at 19:31
  • You should be able to do this: ios_webkit_debug_proxy >> proxy.log 2>&1 and then tail -F proxy.log | grep "Disconnected" Commented Nov 6, 2014 at 19:36

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

6 Comments

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".
I am using OSX. The last one.
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
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!
@KirillZhukov Can you please share the approach you followed? I am running into a similar issue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.