I want to create a pair of bash functions something along the lines of

function generator {
    while [ 1 -le 1 ]
    do
        # run log generator > somefile.log
    done
}

function tail_log {
    generator &
    tail -f somefile.log
}

So, I would run the command tail_log to see the log output. Except, instead of the infinite loop, I'd like allow the user to press Q to terminate the forked process.

So it looks like I need two things:

  1. a way to get the process ID of a forked prcoess
  2. a way to listen for user input so I can kill the process ID when the user presses Q
link|improve this question

78% accept rate
feedback

1 Answer

up vote 4 down vote accepted
  1. $!

  2. Run tail in background as well. In foreground, use the read builtin. On input, kill both background processes.

    While I'm at it:

  3. while [ 1 -le 1 ]while true (see somewhat related section on BashPitfalls)

  4. function foo {...}foo() {...} (see deprecated syntax about this)

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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