none
schtasks: Task status "Could not start", result: -2147024894

    Question

  • Hi

    I'm trying to use schtasks to schedule a nightly stop and start of a service with a command line like "sc.exe stop <service-name> && sc.exe start <service-name>". I'm trying to schedule such a task using schtasks, but I run into a problem running a very simple scheduled task

    schtasks.exe /CREATE /TN FOOBAR /SC DAILY /ST 12:42 /tr "echo foobar > C:\foo.txt" /RU SYSTEM

    schtasks.exe /RUN /TN FOOBAR

    This should create a task to write the string "foobar" into a file "C:\foo.txt". This command line works with a simple `cmd /C echo foobar > C:\foo.txt`, so I can't see how that would go wrong.

    However, the scheduled task does not create the file. If I query the scheduled task, I see the following:

    C:\Users\blaffoy>schtasks.exe /query /tn FOOBAR /v /fo list

    > Folder: \
    > HostName:                             my-machine
    > TaskName:                             \FOOBAR
    > Next Run Time:                        17/12/2014 12:42:00
    > Status:                               Could not start
    > Logon Mode:                           Interactive/Background
    > Last Run Time:                        16/12/2014 12:41:59
    > Last Result:                          -2147024894
    > Author:                               blaffoy-admin
    > Task To Run:                          echo foobar > C:\foo.txt
    > Start In:                             N/A
    > Comment:                              N/A
    > Scheduled Task State:                 Enabled
    > Idle Time:                            Disabled
    > Power Management:                     Stop On Battery Mode, No Start On Batteries
    > Run As User:                          SYSTEM
    > Delete Task If Not Rescheduled:       Enabled
    > Stop Task If Runs X Hours and X Mins: 72:00:00
    > Schedule:                             Scheduling data is not available in this format.
    > Schedule Type:                        Daily
    > Start Time:                           12:42:00
    > Start Date:                           16/12/2014
    > End Date:                             N/A
    > Days:                                 Every 1 day(s)
    > Months:                               N/A
    > Repeat: Every:                        Disabled
    > Repeat: Until: Time:                  Disabled
    > Repeat: Until: Duration:              Disabled
    > Repeat: Stop If Still Running:        Disabled

    I've tried this from both cmd and powershell sessions, as an admin and a normal user, and with and without the `/RU SYSTEM` flag. With no difference.

    What's the problem here? Have I missed something?

    Tuesday, December 16, 2014 12:51 PM

Answers

  • Is it possible to schedule tasks using schtasks.exe without relying on .bat scripts? There would be fewer moving parts if I could specify the whole thing in a command line.

    This is one of the myths that cannot be killed: That commands in batch files will work but not outside a batch file. As with all myths, this one is incorrect.

    Your tasks fails because the command you specified is not an executable:

    echo foobar > C:\foo.txt

    There is no "echo.exe"! "Echo" is a command built into the command processor, same as copy, cd, del. To make your task work you must first invoke the command processor:

    cmd.exe /c echo foobar > C:\foo.txt

    This also ensures that the redirection to foo.txt works. This is important because a scheduled command such as

    ping www.google.com > c:\ping.txt

    would run but it would not send its output to ping.txt. The correct command should be

    cmd.exe /c ping www.google.com > c:\ping.txt

    Why then did the batch file work? Because Windows "knows" that it needs to invoke the Command Processor to run a batch file.

    • Marked as answer by blaffoy Tuesday, December 16, 2014 2:40 PM
    Tuesday, December 16, 2014 2:35 PM

All replies

  • Okay, I made some progress. If I put my task logic in a .bat file then it will work.

    C:\Schtasks\foobar.bat:

    echo foobar > C:\foo.txt 

    > PS C:\Schtasks> schtasks.exe /CREATE /TN "FOOBAR" /SC DAILY /ST 12:36 /tr "C:\Schtasks\foobar.bat" /RU SYSTEM

    > WARNING: The task name "FOOBAR" already exists. Do you want to replace it (Y/N)? Y

    > SUCCESS: The scheduled task "FOOBAR" has successfully been created.

    > PS C:\Schtasks> schtasks.exe /RUN /TN FOOBAR

    > SUCCESS: Attempted to run the scheduled task "FOOBAR".

    This correctly creates the file C:\foo.txt.

    C:\Schtasks\RestartMinion.bat:

    sc.exe stop salt-minion && sc.exe start salt-minion

    > PS C:\Schtasks> schtasks.exe /CREATE /TN "RESTARTSERVICE" /SC DAILY /ST 12:36 /tr "C:\Schtasks\RestartMinion.bat" /RU SYSTEM

    > SUCCESS: The scheduled task "RESTARTSERVICE" has successfully been created.

    > PS C:\Schtasks

    > schtasks.exe /RUN /TN "RESTARTSERVICE"

    > SUCCESS: Attempted to run the scheduled task "RESTARTSERVICE".

    And this correctly restarts my service. Is it possible to schedule tasks using schtasks.exe without relying on .bat scripts? There would be fewer moving parts if I could specify the whole thing in a command line.

    • Edited by blaffoy Tuesday, December 16, 2014 1:54 PM formatting went all screwy
    Tuesday, December 16, 2014 1:52 PM
  • Is it possible to schedule tasks using schtasks.exe without relying on .bat scripts? There would be fewer moving parts if I could specify the whole thing in a command line.

    This is one of the myths that cannot be killed: That commands in batch files will work but not outside a batch file. As with all myths, this one is incorrect.

    Your tasks fails because the command you specified is not an executable:

    echo foobar > C:\foo.txt

    There is no "echo.exe"! "Echo" is a command built into the command processor, same as copy, cd, del. To make your task work you must first invoke the command processor:

    cmd.exe /c echo foobar > C:\foo.txt

    This also ensures that the redirection to foo.txt works. This is important because a scheduled command such as

    ping www.google.com > c:\ping.txt

    would run but it would not send its output to ping.txt. The correct command should be

    cmd.exe /c ping www.google.com > c:\ping.txt

    Why then did the batch file work? Because Windows "knows" that it needs to invoke the Command Processor to run a batch file.

    • Marked as answer by blaffoy Tuesday, December 16, 2014 2:40 PM
    Tuesday, December 16, 2014 2:35 PM
  • So it looks like I caused myself unnecessary grief by trying to use "echo" as a simple command to try and verify that schtasks would work at all.
    Tuesday, December 16, 2014 2:47 PM
  • So it looks like I caused myself unnecessary grief by trying to use "echo" as a simple command to try and verify that schtasks would work at all.

    Good to see that you can look at it with a sense of humor! On the other hand it is a good idea to use a simple command to test a scheduled task.
    Tuesday, December 16, 2014 3:04 PM
  • Yeah, my usual approach to using a tool that I'm unfamiliar with is to test it with something very simple that I understand. Unfortunately I don't really understand cmd so I tripped myself up. The final command line that I used was:

    schtasks.exe /CREATE /TN "RESTARTMINION" /SC DAILY /ST 01:00 /tr "powershell restart-service salt-minion" /RU SYSTEM 


    Tuesday, December 16, 2014 3:20 PM
  • Looks good, with one slight reservation. To make scripts robust one must fully qualify all file paths. Your script file restart-service salt-minion.ps1 has no path, probably because it resides in a folder that is mentioned in the %path% variable. If that mention gets omitted for some reason then the task will fail.
    Tuesday, December 16, 2014 3:48 PM
  • Actually, I think it's fine the way it is. I'm calling the powershell cmdlet `restart-service` on the service named `salt-minion`, so I don't have a .ps1 script file at all.
    Tuesday, December 16, 2014 4:13 PM
  • I don't have a .ps1 script file at all.

    Good point!
    Tuesday, December 16, 2014 5:18 PM