简体   繁体   English

如何将stderr和stdout输出附加到system2 R命令中的文件?

[英]How to append stderr and stdout output to file in system2 R command?

I am trying to execute shell command from R (3.3.1 on Ubuntu) like this: 我试图从R(在Ubuntu上的3.3.1)执行shell命令,如下所示:

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

Unfortunately every time this is executed the contents of log files is overwritten. 不幸的是,每次执行此操作时都会覆盖日志文件的内容。 Can we somehow specify this to perform append instead of overwrite? 我们可以以某种方式指定这个来执行追加而不是覆盖吗?

This performs the way I wanted... 这按我想要的方式进行......

system(command="ls -l /etc/ >> /tmp/stdout.log 2>> /tmp/stderr.log", 
       wait=TRUE)

And this one is even better since it permits the use of more modern system command and permits doing clearer logging. 而且这个更好,因为它允许使用更现代的系统命令并允许更清晰的记录。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM