0

Backstory: I'm working with a piece of software that uses CMake for several build options. On my desktop build system when I execute a 'Processor Reset' command the program exits cleanly, but needs restarted with a different set of parameters. (The functionality works correctly on hardware, so I do not wish to modify the underlying source whatsoever.)

Question: My question is, how can I monitor the terminal output from the program, and upon seeing "Processor Reset Received", trigger a new instance of the program?

Caveats

  • My preferred answer would perform this in a shell script, but I'm open to other ideas.
  • I do not wish for there to be any recursion.

General Flow

  • Start the program with a Power-On Reset

    ./program -PO 
    

    (Program runs and prints output to terminal)

  • Send a Processor Reset Command

  • Program receives Processor Reset Command, prints relevant information, and terminates

  • Monitor program reads that the original program has terminates with a Processor Reset, and runs the program again with different flags.

    ./program -PR
    

Things I've tried

I have a partially working solution that... is interesting. It appears that there is some weird buffering going on which I've tried to disable, albeit unsuccessfully. But - it does successfully parse the program's terminal output and execute the Processor Reset flags when necessary (albeit once, as it is not in a loop.)

stdbuf -o0 -i0 -e0 sudo ./program -PO | tee /dev/tty | (grep "Processor Reset.") | (read && sudo ./program -PR)

To recap

  • I need the code to not buffer large chunks of data (i.e. write smoothly as it produces output)
  • It should be wrapped in a loop to allow multiple Processor Resets. (Any other reset should terminate the loop.)
1

What if you have a shell script like so:

$ cat top.bash
#!/bin/bash

echo "launched top.bash"
./fakeprog1.bash | grep -q "Processor Reset Received" && exec ./fakeprog2.bash

You can replace the ./fakeprog1.bash with your ./program -PO and ./fakeprog2.bash with your ./program -PR.

When I invoke my version it runs like so:

$ ./top.bash
launched top.bash
launching /root/453742/fakeprog2.bash

The fakeprog1.bash echos a message but we cannot see it because it gets filtered out by the grep. Logs can be directed to files + this script via tee so that shouldn't be a issue.

  • I think this is close, however it doesn't allow for multiple Processor Resets? – Ramrod Jul 6 '18 at 12:55
  • @Ramrod - can you explain what you want to happen overall with that? Your example only shows 1 iteration like that. – slm Jul 6 '18 at 13:59
  • My answer below is probably the best way to show what I'm after. But, after looking at it (and the more I learn) I don't think I'm really using tee correctly. That code functions, but buffers very slowly to the terminal screen. – Ramrod Jul 6 '18 at 17:59
0

Thanks to slm for pointing me in the right direction!

# Start the program with Power-On Reset
# Use tee to print output to terminal and pipe to grep
# grep returns 0 if the string is found
sudo ./program -PO | tee /dev/tty | grep -q "Processor Reset."

# Store the grep exit code in a variable
result="$?"

# If result is 0, grep found the Processor Reset string
if [ "$result" == "0" ]; then
  while : ; do
  sudo ./program -PR | tee /dev/tty | grep -q "Processor Reset."
  result="$?"

# This loop will continue as long as the Processor Reset string is found
# once the program terminates
  [ "$result" == "0" ] || break
  done
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.