Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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
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

Your Answer


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.