3

I have a scheduled task that is set to run an R script by launching Rscript.exe and the file of interest.

This task completes successfully when I run it with the option

"Run only when user is logged on".

I need to schedule the task to run without me, so I checked the option:

"Run whether user is logged on or not"

and gave it my password.

I needed to test this and thus clicked "run" only to watch it immediately fail with a result of

0x1

There is a log file created by R when the script runs, however the scheduled task apparently failed before this log was created.

I found this resource, which says that when the task cannot be successfully run manually when the "whether user is logged on or not" option is checked.

With that in mind I instead scheduled the task to run a few minutes in the future. The task again failed with the 0x1 run result before launching Rscript and thus creating a log.

I repeated this process and few times and the result was the same each time.

When I look at the task's properties I see that in 1 second of time it reached the "Action Completed" step which returned a non-zero return code and failed with an Operational Code of "(2)".

enter image description here

The task definition is:

Action: Start a program
Program/script: cmd
Add arguments:

    /c E:/PROGRA~1/R/R-33~1.1/bin/Rscript.exe T://code//Forecast.R  >> T://code//Forecast.log 2>&1
Hack-R
  • 295
  • 3
  • 12
  • Anything in Event Viewer? Can you post the task definition? – dsolimano Aug 10 '16 at 21:56
  • @dsolimano Excellent questions. I will absolutely follow up on these first thing tomorrow morning. – Hack-R Aug 10 '16 at 22:16
  • @dsolimano I've updated the question with the task definition. The task also failed this morning when it tried to run at it's daily scheduled time of 9:00am. I don't see anything related in Event Viewer. – Hack-R Aug 11 '16 at 14:09
  • Hmm, are E and T local drives, or network mappings? If they're network mappings, how do they get set? Might not be getting set by the batch logon process. – dsolimano Aug 11 '16 at 14:12
  • @dsolimano E:/ is local T:/ is a network drive. I probably don't fully understand this aspect in terms of how it gets set; I only know that T:/ is always available in the Explorer on this server and to apps like RStudio on this server. I will try moving the script from T:/ to a local drive and see if that helps. – Hack-R Aug 11 '16 at 14:14
  • Give that a shot, if that works it would be as simple as replacing the t drive with a UNC path I would guess. – dsolimano Aug 11 '16 at 14:15
  • @dsolimano Looks like it had the same result (didn't complete). I changed the arguments to `/c E:/PROGRA~1/R/R-33~1.1/bin/Rscript.exe E://Forecast.R >> E://Forecast.log 2>&1` – Hack-R Aug 11 '16 at 14:20
  • I think this [link](https://social.technet.microsoft.com/Forums/windows/en-US/c03d6691-b058-4f8d-961c-e8eba25bbaed/task-scheduler-problem-run-whether-user-is-logged-on-or-not?forum=w7itprogeneral) has given the solution. When you need to `Run whether user is logged on or not`, you need to also check the `Run with highest privileges` – Simon MC. Cheng Aug 12 '16 at 19:01
  • @SimonMC.Cheng Thanks Simon. i should have mentioned that I already tried that as well :/ I will update my question to reflect that. – Hack-R Aug 12 '16 at 20:31
  • I am thinking you should modify the task definition to `E:/PROGRA~1/R/R-33~1.1/bin/Rscript.exe` then pass `T://code//Forecast.R >> T://code//Forecast.log 2>&1` as argument to see it helps, loading from cmd.exe seems like causing the problem. Besides, I notice you have written some logs to `Forecast.log`, is there any useful information too? – Simon MC. Cheng Aug 13 '16 at 04:04

1 Answers1

1

You need to have the "Log on as a Batch Job" security right to run a task as a user.

Admin Tools -> Local Security Policy -> Local Policies -> User Rights Assignment -> Log On as a Batch Job.

I'm sure in the event viewer you'll see a security event with a failed logon attempt with a logon type of 4 (batch job).

Christopher_G_Lewis
  • 3,685
  • 22
  • 27
  • I added my username to "Log On as a Batch Job" and scheduled it to run in 5 minutes, but it was still unsuccessful with the same result codes as before. I think I was already included in "Log On as a Batch Job" by virtue of some of the user groups I'm in. I don't see anything related in Event Viewer. – Hack-R Aug 11 '16 at 14:43