在 Unix 上的 Tcl 中,如何杀死已使用 exec 启动的后台进程?

发布于 2024-10-17 20:36:19 字数 411 浏览 4 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌梦深 2024-10-24 20:36:19

在 tclsh 环境中,您仍然应该可以访问 pskill。我重新创建了您的循环脚本,然后进入 tclsh 会话:

$ tclsh
% exec /path/to/loop.sh &
22267% ps        
  PID TTY          TIME CMD
19877 pts/0    00:00:00 bash
22212 pts/0    00:00:00 emacs-x
22317 pts/0    00:00:00 tclsh
22319 pts/0    00:00:00 loop.sh
22326 pts/0    00:00:00 sleep
22327 pts/0    00:00:00 ps
% kill 22319
% ps      
  PID TTY          TIME CMD
19877 pts/0    00:00:00 bash
22212 pts/0    00:00:00 emacs-x
22317 pts/0    00:00:00 tclsh
22332 pts/0    00:00:00 ps

如果您想从 tcl 脚本执行此操作,这里有一个简短的示例,它显示了 exec'ed 进程启动后的 ps 结果,然后停止后:

#!/usr/bin/tclsh
set id [exec /path/to/loop.sh &]
puts "Started process: $id"
set ps [exec /bin/ps]
puts "$ps"
exec /usr/bin/kill $id
puts "Stopped process: $id"
set ps [exec /bin/ps]
puts "$ps"

如果您的系统有 ps 和kill 在不同的目录中,您将必须相应地修改脚本。

While in the tclsh environment, you should still have access to commands like ps and kill. I re-created your loop script and then went into a tclsh session:

$ tclsh
% exec /path/to/loop.sh &
22267% ps        
  PID TTY          TIME CMD
19877 pts/0    00:00:00 bash
22212 pts/0    00:00:00 emacs-x
22317 pts/0    00:00:00 tclsh
22319 pts/0    00:00:00 loop.sh
22326 pts/0    00:00:00 sleep
22327 pts/0    00:00:00 ps
% kill 22319
% ps      
  PID TTY          TIME CMD
19877 pts/0    00:00:00 bash
22212 pts/0    00:00:00 emacs-x
22317 pts/0    00:00:00 tclsh
22332 pts/0    00:00:00 ps

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:

#!/usr/bin/tclsh
set id [exec /path/to/loop.sh &]
puts "Started process: $id"
set ps [exec /bin/ps]
puts "$ps"
exec /usr/bin/kill $id
puts "Stopped process: $id"
set ps [exec /bin/ps]
puts "$ps"

If your system has ps and kill in different directories, you will have to modify the script accordingly.

痴者 2024-10-24 20:36:19

有两种方法可以终止后台进程,假设您保存了 PID(为了便于论证,

保存 在名为 pid 的变量中): org/wiki/Kill_%28command%29" rel="nofollow">kill 程序

exec kill $pid

使用 来自Tclx 的kill命令

package require Tclx
kill $pid

两者都有效。两者都允许您选择指定要发送的信号而不是默认信号 (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

exec kill $pid

Use the kill command from Tclx

package require Tclx
kill $pid

Both 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.

不奢求什么 2024-10-24 20:36:19

exec 命令实际上会返回它启动的进程的 PID,如果您 RTM 或在交互式 shell 上尝试这一点,这一点很清楚。

这可以与 GreenMatt 建议的 Kill 命令结合使用。

% set pid [exec echo "hello" &]
25623
% hello
% echo $pid
25623

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.

% set pid [exec echo "hello" &]
25623
% hello
% echo $pid
25623
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文