Windows Server 2008 scheduled task only works with “Run only when user is logged on”

scheduled-tasktask-schedulerwindows-server-2008

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

Best Answer

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).