[R] Terminating a cmd windows from R

Jiefei Wang @zwj|08 @end|ng |rom gm@||@com
Mon Aug 19 17:28:29 CEST 2024


You can use a parallel computing package to do it. I haven't test the
code below (as I do not have a long-running batch file) but I think it
should work:

library(parallel)
cl <- makeCluster(1)
## run the command in a child R process
clusterEvalQ(cl, {
    nm_log <- "c:/nm74g64/run/nmfe74"
    nmi    <- "202"
    nmrun <- paste0("cmd.exe /k ",nm_log,".bat ",nmi,".ctl ",nmi,".lst")
    system(nmrun, invisible = F, show.output.on.console = T, wait = T)
})
## kill it
stopCluster(cl)

Best,
Jiefei

On Sat, Aug 17, 2024 at 11:44 AM SIMON Nicolas via R-help
<r-help using r-project.org> wrote:
>
> Many thanks for your help.
> I will consider the processx package.
>
> Best regards
> Nicolas
>
>
> Le 17 août 2024 à 16:07, Ivan Krylov <ikrylov using disroot.org> a écrit :
>
> 
> В Sat, 17 Aug 2024 11: 47: 30 +0000 SIMON Nicolas via R-help <r-help@ r-project. org> пишет: > nmrun <- paste0("cmd. exe /k ",nm_log,". bat ",nmi,". ctl ",nmi,". lst") You are using the /k option that instructs cmd. exe to keep the command
>
>
> В Sat, 17 Aug 2024 11:47:30 +0000
> SIMON Nicolas via R-help <r-help using r-project.org> пишет:
>
> > nmrun <- paste0("cmd.exe /k ",nm_log,".bat ",nmi,".ctl ",nmi,".lst")
>
> You are using the /k option that instructs cmd.exe to keep the command
> prompt open. Does the batch file contain an explicit "exit" to ensure
> that cmd.exe terminates?
>
> > system(nmrun, invisible = F, show.output.on.console = T, wait = T)
>
> With wait = TRUE, it should be possible to interrupt the process by
> pressing Ctrl+C in the cmd.exe window, but R itself will not be running
> your commands until system() returns (or is interrupted, terminating
> the process). You can specify a timeout for a foreground process using
> the 'timeout' argument of the system() function.
>
> If you'd like to manage a background process, consider the 'processx'
> CRAN package: https://urldefense.com/v3/__https://cran.r-project.org/package=processx__;!!JQ5agg!YxmSgAHp4UgpqrHOlG1PHD6BvfvG1NC92n14SwHd7xlxN3PkMlbOrtc2Kw67JPntISAjxOic-ZZA0kEDop7N_A$
>
> --
> Très cordialement,
> Ivan
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list