Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
605 views
in Technique[技术] by (71.8m points)

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

let's say i have an executable file stored in c:my directorymy file.exe that i would like to initiate near the beginning of my R script, and then terminate near the end of my R script. what are some clean ways do this on a windows platform?

i am aware of R commands like shell and shell.exec, but it's not clear to me that these will allow a clean capture of the process id to then use something like the pskill function. it's also not clear if it makes more sense to run this executable through some sort of pipe conection - or how that pipe would work. this particular executable should be included in my windows PATH as a system variable, so it's conceivable that the system function might be of value here as well.

additional clarification: capturing the process id might be important, because (at least for me) this will be used on a database server's executable -- if multiple database servers are currently running on the same machine, the process shouldn't kill all of them - just the one initialized at the start of the R script.

for extra credit: let's say c:my directorymy file.exe should be called by actually executing another file - c:my directoryanother file.bat - but it's my file.exe that needs to be killed at the end of the R script.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...