2

I'd like to send a keystroke to ranger, when I have ranger running and have gotten it's PID with:

Output of ps aux | grep '[/]usr/bin/python -O /usr/bin/ranger':

chris     5054  3.1  0.1 116248 27828 pts/0    S+   16:57   0:00 /usr/bin/python -O /usr/bin/ranger

Following from https://serverfault.com/a/178470/135542, if I send the value j to it's STDIN with: echo "j" > /proc/5440/fd/0 I just seem to get j mixed in the output (after I ran the command a few times):

enter image description here

However when I press j on my keyboard, it reacts correctly.

1 Answers1

1

This is because, the

echo "j" > /proc/5440/fd/0 

in fact, is something like this (the pts number can be differ in your case):

echo "j" > /dev/pts/2 

Explanation:

I doesn't have ranger, so I were running vim - the no difference here, mechanism the same. Then, I found out the vim's PID - 6466 and looked into the /proc/6466/fd directory:

$ ls -l /proc/6466/fd
lrwx------ 1 minimax minimax 64 Nov 20 01:20 0 -> /dev/pts/2
lrwx------ 1 minimax minimax 64 Nov 20 01:20 1 -> /dev/pts/2
lrwx------ 1 minimax minimax 64 Nov 20 01:20 2 -> /dev/pts/2

All file descriptors of the vim's process are symbolic links to the current terminal. So, the echo "j" > /proc/5440/fd/0 converts to the echo "j" > /dev/pts/2, which means literally: "Redirect the output of the echo command to the terminal file /dev/pts/2". Not to the stdin of the vim's process.

As far as I know, the terminal can get input only from keyboard and there is no way passing input to it somehow else. So, you need search for another method, to passing commands to the working process.

Also, keep in mind, that the echo adds the newline to its output, that is causing printing j by stairs, in your case. If change it to the echo -n j, the output will be jjj - without newlines.

MiniMax
  • 4,123