2016-09-01 2 views
0

私はこのようなR(Ubuntuの上3.3.1)からシェルコマンドを実行しようとしています:stderrとstdoutの出力をsystem2のRコマンドでファイルに追加する方法は?

system2(command="ls", 
     args=c("-l", "/etc/"), 
     stdout="/tmp/stdout.log", 
     stderr="/tmp/stderr.log", 
     wait=TRUE) 

残念ながら、これは、ログファイルの内容を実行するたびに上書きされます。上書きするのではなく、追加を実行するために何とかこれを指定できますか?

+0

'stderr = TRUE'を使用してcharベクトルを取得し、それを' append = TRUE'でファイルに書き出しますか? – Tensibai

+0

残念ながら、そのようにキャプチャできる既知の制限は8095バイトです。コマンドがより多くのデータを出力するとどうなるでしょうか? –

+0

その後、タイムスタンプ付きのファイル以外のオプションは表示されません。 (おそらく、回避策は、 "tempfile"をstdout/stderrとして使用して、 "セントラル"ログを読み書きすることができますか? – Tensibai

答えて

0

そして、それはより近代的なシステムコマンドの使用を許可し、より明確なロギングを行うことが可能ので、この1はさらに良いです。

result <- system2(command="ls", 
        args=c("-l", "/etc/"), 
        stdout="/tmp/stdout.log", 
        stderr="/tmp/stderr.log", 
        wait=TRUE) 
now <- date() 
cat(paste0("Executed: ", now, "\n"), file="/tmp/stdoutmain.log", append=TRUE) 
file.append("/tmp/stdoutmain.log", "/tmp/stdout.log") 
cat(paste0("Executed: ", now, "\n"), file="/tmp/stderrmain.log", append=TRUE) 
file.append("/tmp/stderrmain.log", "/tmp/stderr.log") 
0

これは...私が望んでいた方法を実行

system(command="ls -l /etc/ >> /tmp/stdout.log 2>> /tmp/stderr.log", 
     wait=TRUE) 
関連する問題