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

9,855

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

Share:
9,855

Related videos on Youtube

Hack-R
Author by

Hack-R

Updated on September 18, 2022

Comments

  • Hack-R
    Hack-R over 1 year

    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
    
    • dsolimano
      dsolimano over 7 years
      Anything in Event Viewer? Can you post the task definition?
    • Hack-R
      Hack-R over 7 years
      @dsolimano Excellent questions. I will absolutely follow up on these first thing tomorrow morning.
    • Hack-R
      Hack-R over 7 years
      @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.
    • dsolimano
      dsolimano over 7 years
      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.
    • Hack-R
      Hack-R over 7 years
      @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.
    • dsolimano
      dsolimano over 7 years
      Give that a shot, if that works it would be as simple as replacing the t drive with a UNC path I would guess.
    • Hack-R
      Hack-R over 7 years
      @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
    • Simon MC. Cheng
      Simon MC. Cheng over 7 years
      I think this link 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
    • Hack-R
      Hack-R over 7 years
      @SimonMC.Cheng Thanks Simon. i should have mentioned that I already tried that as well :/ I will update my question to reflect that.
    • Simon MC. Cheng
      Simon MC. Cheng over 7 years
      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?
  • Hack-R
    Hack-R over 7 years
    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.