7

c:\my directory\my file.exeRスクリプトの最初の方で開始し、Rスクリプトの最後の方で終了したい実行可能ファイルが保存されているとします。Windowsプラットフォームでこれを行うためのいくつかのクリーンな方法は何ですか?

私はshellshell.execのようなRコマンドを知っていますが、これらがプロセスIDのクリーンなキャプチャを可能にし、pskill関数のようなものを使用できるかどうかはわかりません。また、この実行可能ファイルをある種のパイプ接続を介して実行する方が理にかなっているのか、またはそのパイプがどのように機能するのかも明確ではありません。PATHこの特定の実行可能ファイルは、システム変数としてWindowsに含める必要があるため、systemここでも関数が価値があると考えられます。

追加の説明:プロセスIDをキャプチャすることは重要かもしれません。これは、(少なくとも私にとっては)データベースサーバーの実行可能ファイルで使用されるためです。複数のデータベースサーバーが現在同じマシンで実行されている場合、プロセスはそれらすべてを強制終了するべきではありません。 -Rスクリプトの開始時に初期化されたものだけ。

c:\my directory\my file.exe追加のクレジットの場合:実際に別のファイルを実行して呼び出す必要があるとしましょう---c:\my directory\another file.batしかしmy file.exe、Rスクリプトの最後でそれを強制終了する必要があります。

4

3 に答える 3

5

受け取った他の2つの回答を利用すると、この手法は、述べられた目標を達成するための合理的な方法のように思われます。

# 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 )
于 2013-02-28T18:00:41.830 に答える
2

関数を使用できますsystem

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

編集: これは、Windows 7 を搭載した私のコンピューターで機能しました (skype.exe を強制終了してテストしました)。

于 2013-02-28T13:05:09.230 に答える
2

以前はpsKillを使用していました。それは本当に強力で、おそらく危険です。リモートコンピューターでもマルチプロセスを強制終了します。残忍なプロセスを殺したいときは、非常に注意する必要があることを知っていると思います。

  1. ツールをダウンロードし、解凍して既知のパスにコピーします。
  2. 初めて起動すると、ライセンスが要求されます..コマンドから一度起動すると、同意します。

次に、このようなものを使用します

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

たとえば、すべてのクロム インスタンスを強制終了するには、次のようにします。

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

編集

同じ名前のマルチプロセスがあると仮定します。pslistこの名前のすべてのプロセスを一覧表示するために使用できます。経過時間に応じて強制終了するプロセスの ID を見つけ、ID で pskill を呼び出します。

たとえば、ここでは最後に起動されたクロムプロセスを殺したい

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)
于 2013-02-28T15:26:23.867 に答える