how to run an executable file and then later kill or terminate the same process with R in Windows

前端 未结 3 800
感情败类
感情败类 2020-12-10 07:02

let\'s say i have an executable file stored in c:\\my directory\\my file.exe that i would like to initiate near the beginning of my R script, and then terminate

相关标签:
3条回答
  • 2020-12-10 07:33

    You could use system function:

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

    edit: This worked in my computer with Windows 7 (tested with killing skype.exe).

    0 讨论(0)
  • 2020-12-10 07:44

    In the past, I used psKill. It is really powerful and maybe dangerous. You kill multi-procees even ina remote computer. I think you konw we must be extremely careful when we want to kill brutally process.

    1. Download the tool , unzip and copy in a known path.
    2. First time you launch is asking for liscence..You launch it from the cmd once and you agree.

    Then you use somthing like this

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

    For example to kill all chrome instances, you do this

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

    EDIT

    Assuming you have multi process with the same name. You can use pslist to list all process with this name. Find the id of the process you want to kill according to its elapsed time, then call pskill by id.

    For example here I want to kill , the last launched chrome process

    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)
    
    0 讨论(0)
  • 2020-12-10 07:49

    drawing on the other two answers received, this technique seems like a reasonable way to accomplish the stated goal..

    # 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 )
    
    0 讨论(0)
提交回复
热议问题