Inject Clicking on another process using Bash

I have a process that runs indefinitely until a key is pressed. I would like to use bash to enter a keystroke into this process so that it ends. Based on this post, linux - write commands from one terminal to another I tried using

echo -e "b" > /proc/[pid]/fd/0

(The letter "b" in this case is just arbitrary). The letter "b" will be displayed in the terminal of the process, which runs indefinitely, but it does not cause the program to terminate, as if it were running, if in fact I find "b" in the window.

I also saw the recommendation for xdotools, but I could not get it to work, and I try to stay away from using the GUI to implement this.

I am running Ubuntu 10.04 and I do not have much experience in bash.

+3
source share
2 answers

From here :

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int main(void)
    {
    int hTTY = open("/dev/tty1", O_WRONLY|O_NONBLOCK);
    ioctl(hTTY, TIOCSTI, "b");

    close(hTTY);
    return 0;
    }

The terminal and keystroke are hardcoded in this example, but can be adapted to your needs.

You can do something like this in Perl:

perl -e '$TIOCSTI = 0x5412; $tty = "/dev/pts/1"; $char = "b"; open($fh, ">", $tty); ioctl($fh, $TIOCSTI, $char)'

I need to run any of them using sudo.

+4
source

how about just killing a process from a script

killall processname
0
source

All Articles