Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question
add comment

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)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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