在 Unix 上的 Tcl 中,如何杀死已使用 exec 启动的后台进程?
我想使用 Tcl 的 exec 命令从 Tcl 程序中启动 Unix 后台进程。但是,我想在将来的任意时间以编程方式终止同一个 Tcl 程序中的后台进程。我怎样才能最好地完成这个任务?
bash$ cat loop.bash
#!/bin/bash
while [ 1 ]; do sleep 5; done;
bash$ tclsh
% exec /home/dana/travis/loop.bash &
6598
% puts "How do I kill the background process started by the previous exec command?"
How do I kill the background process started by the previous exec command?
%
I would like to launch a Unix background process from within a Tcl program by using Tcl's exec command. However, I'd like to programmatically kill the background process from the same Tcl program at some arbitrary time in the future. How can I best accomplish this?
bash$ cat loop.bash
#!/bin/bash
while [ 1 ]; do sleep 5; done;
bash$ tclsh
% exec /home/dana/travis/loop.bash &
6598
% puts "How do I kill the background process started by the previous exec command?"
How do I kill the background process started by the previous exec command?
%
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 tclsh 环境中,您仍然应该可以访问
ps
和kill
。我重新创建了您的循环脚本,然后进入 tclsh 会话:如果您想从 tcl 脚本执行此操作,这里有一个简短的示例,它显示了 exec'ed 进程启动后的 ps 结果,然后停止后:
如果您的系统有 ps 和kill 在不同的目录中,您将必须相应地修改脚本。
While in the tclsh environment, you should still have access to commands like
ps
andkill
. I re-created your loop script and then went into a tclsh session:If you're wanting to do this from a tcl script, here is a short example which shows the results of ps after the exec'ed process has been started and then after it has been stopped:
If your system has ps and kill in different directories, you will have to modify the script accordingly.
有两种方法可以终止后台进程,假设您保存了 PID(为了便于论证,
保存 在名为
pid
的变量中): org/wiki/Kill_%28command%29" rel="nofollow">kill 程序使用
来自
Tclx 的kill
命令两者都有效。两者都允许您选择指定要发送的信号而不是默认信号 (SIGTERM)。两者都可以让您同时向多个进程发送信号。
There are two ways to kill a background process, assuming you saved the PID (in a variable called
pid
for the sake of argument):Run the kill program
Use the
kill
command from TclxBoth work. Both let you optionally specify a signal to send instead of the default (SIGTERM). Both let you send the signal to more than one process at once.
exec 命令实际上会返回它启动的进程的 PID,如果您 RTM 或在交互式 shell 上尝试这一点,这一点很清楚。
这可以与 GreenMatt 建议的 Kill 命令结合使用。
The exec command will actually return the PID of a process it starts, which is clear if you RTM or try this on an interactive shell.
This can be used in combination with the kill command as suggested by GreenMatt.