Darko Miletic
Darko Miletic

Reputation: 1356

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:

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?

Upvotes: 2

Views: 1287

Answers (2)

Darko Miletic
Darko Miletic

Reputation: 1356

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

Upvotes: 1

Darko Miletic
Darko Miletic

Reputation: 1356

This performs the way I wanted...

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

Upvotes: 0

Related Questions