0

Objective: Have program running in the background (with a PID), capture the PID, collect output and kill PID.

My code:

tee -a log | program | tee -a log &
PID=$!
disown $PID
sleep 10
pkill '^program*' 2> /dev/null

I ran a variation of different redirect options but I can't seem to have the output being sent to "log".

I've also tried an approach mentioned in a another thread, that recommended the usage of:

program &> log & 

Even though that runs fine, it can't seem to capture the output of "program".

"program" is also interactive in a way that it will, for some seconds, output commands, that's why I have the sleep 10 to give it enough time to finish.

Any tips?

Thank you!

  • if program &> file doesn't seem to capture the output, what happens to the output then? – ilkkachu Apr 14 '17 at 17:00
  • It is not displayed on the screen nor is it sent to the file. I assumed that using program &>> file would address it, but also didn't add anything to log. – t988GF Apr 14 '17 at 17:06
0

With expect:

#!/usr/bin/env expect

package require Tcl 8.5

proc time_to_die {sid} {
    close $sid
    # if close is insufficient (e.g. the program is badly behaved) may
    # need to instead get and blast away at the pid
    #set pid [exp_pid $sid]
    #exec kill $pid
    exit
}

# spam output to this here file
log_file log

spawn -noecho TODOyourprogramhereFIXME

# and this here is in milliseconds
after 10000 [list time_to_die $spawn_id]

vwait godot

Which should run the program in a PTY, collect its output via the log_file call, and after 10 seconds close it. If there is distinctive output that happens before the 10 seconds are up, one could instead detect that and close-or-kill the program when that happens with a more typical expect detection of program output.

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.