有没有办法可以向程序发送Ctrl+C信号tcl?
我有一个tcl code当我执行它时,在内部它应该通过Ctrl+C信号并打印类似的内容:
将“sent ctrl+c”放入同一文件中。
proc abc {
# Want to sent ctrl + c"
Here I want the command for ctrl+c
puts " sent ctrl+c"
}
Run Code Online (Sandbox Code Playgroud)
如果您将信号发送到 Expect 控制下的程序,您将执行以下操作:
\n\nsend "\\003"\nRun Code Online (Sandbox Code Playgroud)\n\nCtrl这实际上是当您执行+时键盘立即生成的字符C;它被终端驱动程序转换成信号。
\n\n否则,您需要使用 TclX 包(或 Expect,但只有在需要其完整功能时才应该使用它),它提供了一个kill命令:
package require Tclx\n\nkill SIGINT $theProcessID\n# You could also use INT or 15 to specify the signal to send.\n# You can provide a list of PIDs instead of just one too.\nRun Code Online (Sandbox Code Playgroud)\n\n知道要发送到哪个进程 ID 就是在创建进程时跟踪事物的问题。pid如果您不给它任何参数,则命令将返回当前进程的 PID 。exec ... &对于它创建的后台管道中的所有(已知)进程,返回所创建的子进程的进程 ID 。对于使用 创建的管道open |...,请将管道的通道句柄传递给pid命令以获取子进程 ID。
set pipeline [open |[list program1 ... | program2 ... | program3 ...] "r+"]\nputs $pipeline "here is some input"\nset outputLine [gets $pipeline]\n\nkill SIGINT [pid $pipeline]\n\n# This close *should* probably produce errors; you\'ve killed the subprocesses after all\ncatch {close $pipeline}\nRun Code Online (Sandbox Code Playgroud)\n\n如果您正在处理中断信号,请使用signalTclX 中的命令来执行此操作:
package require Tclx\n\nsignal error SIGINT; # Generate a normal Tcl error on signal\nsignal trap SIGINT {; # Custom signal handler\n puts "SIGNALLED!"\n exit\n}\nsignal default SIGINT; # Restore default behaviour\nRun Code Online (Sandbox Code Playgroud)\n\n如果您使用signal error SIGINT,则生成的错误将包含此消息 \xe2\x80\x9c SIGINT signal received\xe2\x80\x9d 和此错误代码 \xe2\x80\x9c POSIX SIG SIGINT\xe2\x80\x9d。这很容易测试(特别是使用 Tcl 8.6 的try \xe2\x80\xa6 trap \xe2\x80\xa6命令)。