Inject Keystroke to different process using Bash

前端 未结 2 2057
無奈伤痛
無奈伤痛 2020-12-18 13:06

I have a process that runs indefinitely until a key is pressed. I would like to use bash to inject a keystroke into this process to have it terminate. Based on this post, li

相关标签:
2条回答
  • 2020-12-18 13:44

    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 it can be adapted to your needs.

    You can do something similar in Perl:

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

    I have to run either of these with sudo.

    0 讨论(0)
  • 2020-12-18 13:46

    what about just killing the process from a script

    killall processname
    
    0 讨论(0)
提交回复
热议问题