Eric Krantz
Eric Krantz

Reputation: 2189

R function to determine if another application is running

I have an R script that reads a file from my synced OneDrive folder. The file contains data from a remote computer that is updated hourly. I run the R script daily. Sometimes I forget to open OneDrive to sync files before I run the R script. How do I ask R from within my script if OneDrive (or another software) is running? Using Windows 7/10 and the R 3.5.1.

Also, is there a generalized command to determine from within an R script whether application XYZ is running?

Upvotes: 2

Views: 482

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269321

On Windows run this. If OneDrive is running then task will contain a line of information having the substring OneDrive.exe; otherwise, that substring will not be present.

task <- shell('tasklist /fi "imagename eq OneDrive.exe" /nh /fo csv', intern = TRUE)
grepl("OneDrive.exe", task) # returns TRUE if OneDrive running; else FALSE

This can be generalized by factoring out the image name:

imageName <- "OneDrive.exe" # change this line
cmd <- sprintf('tasklist /fi "imagename eq %s" /nh /fo csv', imageName)
task <- shell(cmd, intern = TRUE)
grepl(imageName, task)

Alternately use schtasks instead of tasklist, use taskscheduler_ls() in the taskscheduleR package or see returning command line from windows tasklist .

Upvotes: 8

Related Questions