0

I have created an script which saved down an excel file and sends it attached to an email.

I create a to automate the process so that it can be run each Sunday:

@echo off
set JAVA_HOME = "C:\Program Files\Java\jre1.8.0_151"

"C:\Program Files\R\R-3.4.3\bin\x64\Rscript.exe" Report.r -multiwindow
exit /B

Then, I create the task in the Task Scheduler.

In the security options at the General tab I check Run whether user is logged on or not - Run with highest privileges. In Triggers, I add a new trigger and I check Weekly, and Advanced settings, Enabled. In Actions, I browse the .bat file and I save the task.

When I click Run, nothing happens, but it does say Task completed, despite both the R script and batch file runnnig fine outside of the scheduled task.

Any ideas?

Compo
  • 36,585
  • 5
  • 27
  • 39
NinaS
  • 3
  • 5
  • I would try to schedule using this package. https://github.com/bnosac/taskscheduleR. If that still doesn't work, add logging to your script. It could be failing and that is why it is saying Task Completed but not outputting anything. Last time I ran into this problem, I was trying to access a DB in my script and the account used by the Task Scheduler could not access the DB. Task Scheduler doesn't know when R fails, it just knows the task completed. – Andrew Troiano Apr 30 '19 at 13:35
  • 2
    `set JAVA_HOME = "C:\Program Files\Java\jre1.8.0_151"` is wrong, it sets a variable named `JAVA_HOME` to the value `"C:\Program Files\Java\jre1.8.0_151"`. You need to remove the spaces either side of the **`=`** character. – Compo Apr 30 '19 at 13:50
  • @Compo I changed it, but it doesn't make any difference. At the same time, I have another 10 tasks that use exactly the same batch script (only thing that changes is the name of the R script) and they all run fine. – NinaS Apr 30 '19 at 13:57
  • @AndrewTroiano So, are you saying the R script fails? – NinaS Apr 30 '19 at 13:58
  • When you run this as a scheduled task, it is usual that the current directory is not what you intended it to be. You must therefore set the current directory at the top, `CD /D "My Known location"`, or use the full path to your `.r` script, `"%ProgramFiles%\R\R-3.4.3\bin\x64\Rscript.exe" "C:\MyDir\MySubDir\Report.r" -multiwindow`. You may, if it is in the same location as the batch file itself, use `%~dp0` to reference the `d`rive and `p`ath, _(with trailing backslash)_ of the batch file. – Compo Apr 30 '19 at 14:03
  • What Compo says about setting `java_home` is quite correct. It's quite possible that `java_home` is established by default in the task's environment. To verify, simply execute `set java` and examine the resultant report.. I'd suggest adding a `pause` line before the `exit/b` line to hold the window open so you can see any errors reported. – Magoo Apr 30 '19 at 15:05
  • @NinaS I would see what a logged output from the R script says, if it logs an error, it will help you figure out what is going wrong in the R script. – Andrew Troiano Apr 30 '19 at 15:18
  • Please read [Why is no string output with 'echo %var%' after using 'set var = text' on command line?](https://stackoverflow.com/a/26388460/3074564) `set JAVA_HOME = "C:\Program Files\Java\jre1.8.0_151"` is definitely wrong. Better would be `set "JAVA_HOME=C:\Program Files\Java\jre1.8.0_151"` with no spaces around equal sign and with first `"` left to variable name to assign the folder path without double quotes to environment variable `JAVA_HOME`. I would further recommend to check first if that directory exists at all. What about just 32 bit Java is installed and not 64 bit Java? – Mofi May 01 '19 at 09:42
  • `Report.r` specified without path is searched in current directory. On double clicking a batch file Windows makes usually (not always) the directory containing the batch file the current directory. A scheduled task is executed in `%SystemRoot%\System32` as current directory except __Start in__ property of scheduled task defines a different directory. See also [What must be taken into account on executing a batch file as scheduled task?](https://stackoverflow.com/a/41821620/3074564) So change last but one line to `"%ProgramFiles%\R\R-3.4.3\bin\x64\Rscript.exe" "%~dp0Report.r" -multiwindow`. – Mofi May 01 '19 at 09:47

0 Answers0