Writing into STDIN of an already running process
I have the following problem: I started a fsck. It ran for a few hours. And now I notice, that I forgot to give it the "-y" option. Now fsck is asking me the same yes/no question a million times for each broken block again and is expecting me to type "y" a million times. What to do?
# pgrep fsck 6503 # while true; do echo "j"; sleep 0.1; done | script -q -c "reptyr 6503" /dev/null
first I get the PID of the fsck process
then I generate many many "j"s in a loop. I'm running fsck in a german speaking environment, so it is expecting me to answer "ja" ( instead of the english "yes"), that's why I send "j"s instead of "y"s.
I could have used the "yes" command instead. However then fsck (in my case fsck.ext4) gets stuck. I'm guessing that "yes" is too fast and somehow fills up fsck's input buffer or something similar. So I generate the "j"s myself and wait a bit before sending the next "j".
scriptwill run a command in a "real" tty, like it was an interactive terminal. If we don't do this, then reptyr will complain that: "[!] Unable to read terminal attributes: Inappropriate ioctl for device"
we run script with -q, because we don't care about its output
we tell script to write its output to /dev/tty
scriptexecute the command
reptyrwhich will steal STDIN/STDOUT/STDERR from the given PID/command and reattach the STDIN/STDOUT/STDERR to the current process - us.
So finally we can answer one million times "j" to fsck which is waiting for our answer...
It's amazing that one is able to re-steal the
reptyr itself again. Once my solution
yes got stuck, I fixed it with the
echo; sleep loop and reptyr'ed.
That's a sign of a well though out and cleanly executed abstract idea.
Tomáš Pospíšek, 2015-05-25