I encountered a problem the other day while running multiple tty sessions in separate terminal windows. I was running a expensive disk access command and was waiting for it to return. Meanwhile I was working in another tty session in a separate window in my graphical desktop environment.

Inadvertently I focused the terminal that was running the disk access command (basically a cp command) and typed the following into the terminal thinking I was going to enter it into my other window:


Based on this question I knew that the data was now sitting in the STDIN data stream buffer waiting to be run immediately upon the return of the first cp command. And as you know I just told the command to run again :/ .

The Question

Since I had some time (this command was taking > 30 mins to return), is there a way I could have flushed these characters out of the STDIN buffer of that tty before bash reran the process it was finishing?

Or is there another way, other than trying to flush data out of the file stream buffer of another bash instance, that I could avoid having the data evaluated as soon as the process that I was waiting on finished?

  • You can write a small C program to do this, using tcflush: see How can I flush unread data from a tty input queue on a UNIX system. If you ask a slightly different question, how can I remove data from the STDIN stream buffer of the same TTY session, the answer is simple: type Ctrl-Z, which will suspend the command and flush input, then fg to continue it. Commented Apr 24, 2015 at 14:38
  • Wow, i didn't realize Ctl-Z would also flush the input buffer of a running process. Think i will test this... Yeah hopefully you know what I want to do from the question, I was not sure what the best way to frame the question honestly...
    – 111---
    Commented Apr 24, 2015 at 14:43

2 Answers 2


If your tty has the noflsh flag turned off - you can check with

stty -a < /dev/pts/whatever | grep -e -noflsh

from another tty, but the default is that it's turned off - then typing the interrupt, quit, or suspend character will flush the input queue. So if you type Ctrl-Z, then fg, it will suspend the currently running command, flush the input, then resume the command.

  • I just checked a few different machines of mine with the stty command. The grep on one of my machines returned with the following: isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt... does that mean that the -noflsh flag is on or off?
    – 111---
    Commented Apr 24, 2015 at 15:07
  • -noflsh means that it's off, so you're good to go. Commented Apr 24, 2015 at 16:16

Probably you should send SIGSTOP to bash in order to prevent it from doing anything, and after finishing your task send SIGKILL to bash.

Note that SIGSTOP prevents bash from waiting for your task to be finished, and you'll get zombie. After SIGKILL init will reparent your task, and zombie will disappear.

  • Also you may attach to bash process using gdb -p PID and forcely close stdin file descriptor with "p close(0)" command. Commented Apr 24, 2015 at 13:41
  • So just testing the first suggestion, I don't seem to be able to stop one instance of bash from receiving commands with kill -18 PID from another instance of bash where PID is the process id of the instance i want to stop. I can still type in commands and the first instance evaluates them.
    – 111---
    Commented Apr 24, 2015 at 13:53
  • As for the gdb stuff, I was really hoping for something more elegant... I found that method, but the GNU Project Debugger seems a bit overkill to do this. I don't have that installed on any of my machines.
    – 111---
    Commented Apr 24, 2015 at 13:56
  • SIGSTOP is 19th signal, not 18th. If bash runs under another user, consider using sudo. Commented Apr 24, 2015 at 13:59
  • Yeah i had a typo in the comment, i edited it and fixed that, the other instance of bash is running under the same user. sudo doesn't seem to make a difference.
    – 111---
    Commented Apr 24, 2015 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .