如何杀死一个用open()打开的进程 [英] How to kill a process opened with open ()

查看:35
本文介绍了如何杀死一个用open()打开的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图杀死我在 Win32 (XP) 上的 Perl 后台打开的进程我尝试了几件事......似乎没有工作......

I am trying to kill a process that I opened in background in Perl on Win32 (XP) I tried several things.... not seems to be working....

$pid = open( CMD, "| cmd.exe > C:\\cmdout.txt" );

为了杀死这个后台进程尝试了几件事....:(

To Kill this background process tried several things....:(

  1. system('taskkill/F/IM cmd.exe');

system("taskkill/F/pid $pid");

关闭CMD ||警告cmd退出$?";

选项 2 从来没有工作试图打印 PID 打印的值与系统中的实际值不同.选项 1 有效,但我觉得该过程仍在后台运行,因为在我结束该过程后,我等待一段时间并重新启动该过程...

Option 2 never works tried to print the values of PID print and actual in system are different. Option 1 works but I feel that the process is still running in the back ground because after I end the process I wait for some time and re start the process...

请帮忙

-谢谢

推荐答案

我对你的案例有点兴趣.显然,两个 cmd.exe 已启动,父级与 open 返回的 PID 及其子级执行命令.

I played a bit with your case. Apparently two cmd.exe are started, parent with PID returned by open and its child doing commands.

第二种情况部分有效,它杀死了父级,但子级仍在运行.使用 /T 选项 taskkill 你可以强制它杀死所有孩子:

Second scenario partially works, it kills the parent, but child remain running. Using /T option to taskkill you can force it to kill all children:

system("taskkill /F /T /pid $pid");

您将看到有关终止两个进程的消息:

You will see message about termination of both processes:

SUCCESS: The process with PID 3956 child of PID 1864 has been terminated.
SUCCESS: The process with PID 1864 child of PID 580 has been terminated.

第三种情况对我来说似乎很好用.关闭文件句柄会使两个 CMD 进程都死掉.

Third scenario seems to work fine for me. Closing the filehandle makes both CMD processes to die.

这篇关于如何杀死一个用open()打开的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆