bnosac / taskscheduleR

Schedule R scripts/processes with the Windows task scheduler.
331 stars 72 forks source link

schedule tasks ONLOGON fails #16

Closed david-jankoski closed 7 years ago

david-jankoski commented 7 years ago

Hi there,

I've been trying to use your nice package to schedule some daily tasks but I ran into some problems. Namely, when testing with a dummy script that just contains a call to runif(100) , everything is alright as long as the schedule is set to ONCE.

taskscheduleR::taskscheduler_create(
                                    taskname = "test_scheduler", 
                                    rscript = "C:/Users/DavidJankoski/Desktop/scheduler_test.R", 
                                    schedule = "ONCE", 
                                    Rexe = "C:/Program Files/R/R-3.3.0/bin/Rscript.exe", 
                                    starttime = format(Sys.time() + 32, "%H:%M"))

#[1] "SUCCESS: The scheduled task \"test_scheduler\" has successfully been created."

Script goes through fine. However, when i set the schedule = ONLOGON then it breaks with the following error msg

taskscheduler_create(taskname = "test_scheduler", 
                     rscript = "C:/Users/DavidJankoski/Desktop/scheduler_test.R", 
                     schedule = "ONLOGON", 
                     Rexe = "C:/Program Files/R/R-3.3.0/bin/Rscript.exe", 
                     starttime = Sys.Date() + 1)

# error message
[1] "ERROR: Access is denied.\r"
attr(,"status")
[1] 1
Warning message:
running command 'schtasks /Create /TN "test_scheduler" /TR "cmd /c C:/Program Files/R/R-3.3.0/bin/Rscript.exe C:/Users/DavidJankoski/Desktop/scheduler_test.R  >> C:/Users/DavidJankoski/Desktop/scheduler_test.log 2>&1" /SC ONLOGON ' had status 1 

Seems like some administrator issues? Here's my session info, I'm running win10 home v1607, tried it on MRO 3.3.2 and R 3.3.0, as well as setting explicitly the Rexe path to standard R (not MRO) and i get the same error. Also i tried consulting the docs in your package (running cmd line) but without any success (/SC was not recognized as an arg somehow). Any ideas on what could be going wrong?

thanks, david

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] RevoUtilsMath_10.0.0

loaded via a namespace (and not attached):
[1] RevoUtils_10.0.2  tools_3.3.2       data.table_1.10.4 taskscheduleR_1.0
jwijffels commented 7 years ago

you can not have 2 tasks with the same name, are you sure you removed the task 'test_scheduler' before you created a new task with the same name 'test scheduler'? Also, do you have admin rights as the README indicates?

david-jankoski commented 7 years ago

Hi - thanks for getting back so quick.

I did remove the previously scheduled ONCE task and tried the following:

  1. scheduling the same stuff only with ONLOGON
  2. different date formats (Sys.Date()+1 & as well as formatting variations of it)
  3. without a date at all (guessing it will default to today, which also works for me)
    but nothing seemed to work. Here is a reproducible example
# script that will be scheduled - "test_schr.R" --------------

x <- runif(10)
sink("C:/Users/David_Jankoski/Desktop/test_sch1.txt")
x
sink()

then i tried the following

library("taskscheduleR")

# ONCE works ------------------------
taskscheduler_create(taskname = "test1", 
                 rscript = "C:/Users/David_Jankoski/Desktop/test_sch.R", 
                 schedule = "ONCE", 
                 starttime = format(Sys.time() + 62, "%H:%M")
                 )

#> [1] "SUCCESS: The scheduled task \"test1\" has successfully been created."

# remove ONCE task
taskscheduler_delete(taskname = "test1")

#> SUCCESS: The scheduled task "test1" was successfully deleted.

# ONLOGON fails -----------------------
taskscheduler_create(taskname = "test2", 
                 rscript = "C:/Users/David_Jankoski/Desktop/test_sch.R", 
                 schedule = "ONLOGON",
                 starttime = Sys.Date() + 1
                 )

# The Error --------------------------
[1] "ERROR: Access is denied.\r"
attr(,"status")
[1] 1
Warning message:
running command 'schtasks /Create /TN "test2" /TR "cmd /c C:/PROGRA~1/MICROS~1/MRO-33~1.2/bin/Rscript.exe C:/Users/David_Jankoski/Desktop/test_sch.R  >> C:/Users/David_Jankoski/Desktop/test_sch.log 2>&1" /SC ONLOGON ' had status 1 

I double checked the admin rights and I do have them. Also - I am able to schedule the ONCE task successfully - i guess that would result in an error otherwise right? Let me know if i can provide some more information.

thanks, david

jwijffels commented 7 years ago

Did you ask your System administrator to make sure you have the rights to execute Schtasks.exe? ONLOGON is different than e.g. DAILY. ONLOGON means when you start your computer. Maybe your rights there are different than when your computer is already on.

jwijffels commented 7 years ago

have you also tried the examples in the README: https://github.com/bnosac/taskscheduleR

david-jankoski commented 7 years ago

I managed to find the reason - your previous comment

ONLOGON means when you start your computer. Maybe your rights there are different than when your computer is already on

was a great hint and also due to my limited knowledge in these things...so even if i have admin account and all possible rights, i still had to explicitly start rstudio with admin rights in order to be able to do this. all the examples work and ONLOGON goes through successfully.

Sorry again, and thanks for all the help!

cheers, david

david-jankoski commented 7 years ago

p.s. i think it might be helpful to just include a sentence in the docs that in case one wants to schedule ONLOGON tasks - make sure you start R/RStudio as an admin. You know - for people like me :)

jwijffels commented 7 years ago

good that it is solved added a remark in the readme