Echo Control C character

后端 未结 5 1137
醉酒成梦
醉酒成梦 2020-12-17 10:22

I need to grep the output of a third party program. This program dumps out data but does not terminate without pressing ^c to terminate it.

I am currently searching

相关标签:
5条回答
  • 2020-12-17 10:25

    No, piping a CTRL-C character into your process won't work, because a CTRL-C keystroke is captured by the terminal and translated into a kill signal sent to the process.

    The correct character code (in ASCII) for CTRL-C is code number 3 but, if you echo that to your program, it will simply receive the character from its standard input. It won't cause the process to terminate.

    If you wanted to ease the task of finding and killing the process, you could use something like:

    ./program_that_does_not_terminate &
    pid=$!
    sleep 20
    kill ${pid}
    

    $! will give you the process ID for the last started background process.

    0 讨论(0)
  • 2020-12-17 10:31

    Try the following:

    expect -c "send \003;"
    
    0 讨论(0)
  • 2020-12-17 10:31

    I think, you will need something more complex here. If you want to capture the output of the program you can't send the kill signal immediately to the program. Compared to your example above, the del command waits for input, while CTRL+C is no direct input but a kill signal.

    I would suggest a solution where you identify if all output is captured and send the kill signal afterwards. Hoever, the point in time when all output can only be determined by you.

    0 讨论(0)
  • 2020-12-17 10:40

    In Bash, a ^C can be passed as an argument on the command-line (and thus passed to echo for echoing) by writing $'\cc'. Thus, the desired command is:

    echo $'\cc' | ./program_that_does_not_terminate
    
    0 讨论(0)
  • 2020-12-17 10:43

    As @paxdiablo said, CTRL-C keystroke is captured by the terminal and translated into a kill signal sent to the process. So if your process is attached to a terminal, you can send $'\cc' provided by @jwodder to that terminal, letting the terminal send actual kill signal to the process.

    Step:

    1.Compile the C file in Execute a command in another terminal via /dev/pts

    2.sudo ./a.out -n /dev/pts/0 $'\cc' # change /dev/pts/0 to the tty your process attached to

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