开发者

Echo Control C character

I need t开发者_如何学运维o 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 and killing it using its pid. However, I was wondering however if it were possible to echo the control C character. Pseudo code would look like

echo ^c | ./program_that_does_not_terminate

I am able to do something like this in DOS, so there must be a way in linux.

C:\>echo y | del C:\tmp\*
C:\tmp\*, Are you sure (Y/N)? y
C:\>


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.


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


Try the following:

expect -c "send \003;"


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜