如何运行一个可执行文件,然后在Windows中终止与R相同的进程

比方说,我有一个可执行文件存储在c:\my directory\my file.exe ,我想在我的R脚本的开始附近启动,然后终止在我的R脚本的末尾附近。 有什么干净的方式在Windows平台上做到这一点?

我知道像shellshell.exec的R命令,但是我不清楚这些将允许干净的捕获进程ID然后使用类似pskill函数的东西。 通过某种pipe道连接运行这个可执行文件是否更有意义,或者pipe道如何工作,也是不清楚的。 这个特定的可执行文件应该作为系统variables包含在我的windows PATH中,所以可以想象system函数也可能在这里有用。

额外的说明:捕获进程id可能是重要的,因为(至less对我来说)这将用于数据库服务器的可执行文件 – 如果多个数据库服务器当前运行在同一台机器上,进程不应该杀死所有的 – 只是在R脚本开始时初始化的那个。

额外的功劳:让我们说c:\my directory\my file.exe应该被调用通过实际执行另一个文件 – c:\my directory\another file.bat – 但它是my file.exe需要被杀死在最后的R脚本。

你可以使用system功能:

 system("Taskkill /IM myfile.exe /F") 

编辑:这工作在我的电脑与Windows 7(测试与杀死skype.exe)。

过去我用psKill 。 这真的很强大,也许是危险的。 即使在远程计算机中也可以杀死多个进程。 我想你知道,当我们想要杀死残酷的过程时,我们必须非常小心。

  1. 下载该工具,解压并复制到已知的路径中。
  2. 你第一次启动的时候就是要求清晰。你从cmd启动一次,你就同意了。

然后你使用这样的东西

 process_name <- 'your_process_name' system(paste(path_to_pskil,'pskill ',process_name,sep=''),intern=T) 

例如,要杀死所有的chrome实例,可以这样做

 system('c:/temp/pskill chrome',intern=T) !! 

编辑

假设你有多个同名的进程。 您可以使用pslist列出所有具有此名称的进程。 找到你想要杀死的进程的ID,根据其消逝的时间,然后通过ID调用pskill。

例如在这里我想杀死最后一次启动的chrome进程

 my.process <- system('c:/temp/pslist chrome ',intern=T)[-c(1:8)] my.process [1] "chrome 3852 8 38 1052 141008 0:01:58.108 1:43:11.547" [2] "chrome 5428 8 11 202 220392 0:02:08.092 1:43:11.359" [3] "chrome 6228 8 9 146 73692 0:01:58.467 1:43:00.091" [4] "chrome 6312 6 9 130 45420 0:00:08.704 1:17:30.153" [5] "chrome 360 6 9 127 29252 0:00:01.263 0:57:01.084" [6] "chrome 5032 6 9 126 29596 0:00:00.717 0:31:39.875" [7] "chrome 2572 8 9 120 23816 0:00:00.452 0:19:10.307" ## ids are orderd according to the elpased time ## I use tail to get the last one ## some regular expression to get the id from the string ## mine is ugly but I am sure you can do better. id <- substr(gsub("([^[:digit:]]*)", "", tail(my.process,1)),1,4) system(paste('c:/temp/pskill ', id) ,intern=T) 

借鉴收到的另外两个答案,这种技术似乎是达到既定目标的合理方法。

 # specify executable file exe.file <- "C:\\Users\\AnthonyD\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe" # capture the result of a `tasklist` system call before.win.tasklist <- system2( 'tasklist' , stdout = TRUE ) # store all pids before running the process before.pids <- substr( before.win.tasklist[ -(1:3) ] , 27 , 35 ) # run the process shell.exec( exe.file ) # capture the result of a `tasklist` system call after.win.tasklist <- system2( 'tasklist' , stdout = TRUE ) # store all tasks after running the process after.tasks <- substr( after.win.tasklist[ -(1:3) ] , 1 , 25 ) # store all pids after running the process after.pids <- substr( after.win.tasklist[ -(1:3) ] , 27 , 35 ) # store the number in the task list containing the PIDs you've just initiated initiated.pid.positions <- which( !( after.pids %in% before.pids ) ) # remove whitespace after.tasks <- gsub( " " , "" , after.tasks ) # find the pid position that matches the executable file name correct.pid.position <- intersect( which( after.tasks %in% basename( exe.file ) ) , initiated.pid.positions ) # remove whitespace correct.pid <- gsub( " " , "" , after.pids[ correct.pid.position ] ) # write the taskkill command line taskkill.cmd <- paste( "taskkill" , "/PID" , correct.pid ) # wait thirty seconds (so the program fully loads) Sys.sleep( 30 ) # kill the same process that was loaded system( taskkill.cmd )