How to execute a scheduled task with "schtasks" without opening a new command line window?
Asked Answered
U

2

10

I have a batch file that creates a scheduled task using schtasks like this:

schtasks /create /tn my_task_name 
                 /tr "...\my_path\my_task.bat"
                 /sc daily
                 /st 10:00:00 
                 /s \\my_computer_name 
                 /u my_username    
                 /p my_password

It works OK except the fact that when my_task.bat is executed - a new command line window is opened (and closed after execution).

I would like to avoid opening this new window (i.e. to run the task in quiet mode, in the background).

I thought to use

start /b ...\my_path\my_task.bat

but I don't know how, because since I have to call start from the batch file I need to precede it with cmd /c, which again causes the new window to open.

How could I solve this problem ?

Unseal answered 2/10, 2010 at 15:44 Comment(0)
T
5

You can use the Windows Shell Scripting extensions to execute a batch file in invisible mode.

  • Create a plain text file and name it <scriptname>.vbs

  • Paste the following code in the .vbs file

      Set WshShell = CreateObject("WScript.Shell") 
      WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
      Set WshShell = Nothing
    
  • Change the name and path of you batch file according to your need

  • Save the .vbs file and schedule the same in schtasks

Trodden answered 2/10, 2010 at 15:51 Comment(2)
Thanks, I'll try this. But I still wonder if it is impossible to do with a batch file...Unseal
This does not answer OP's question. The proper solution is in the answer below from @mpod.Bachelor
M
6

You can do this by specifying /RU option for schtasks. This option

specifies the user account (user context) under which the task runs. For the system account, valid values are "", "NT AUTHORITY\SYSTEM" or "SYSTEM".

And thus, try this

schtasks /create /tn my_task_name 
                  ....
                 /st 10:00:00 
                 /ru "SYSTEM"
                 ....
Meal answered 8/2, 2011 at 14:46 Comment(1)
Is there a way to do this if you don't have system privileges? I have script that works if I schedule it in the foreground, but when I add the /ru "SYSTEM" argument I get this error: ERROR: Access is denied.Catharina
T
5

You can use the Windows Shell Scripting extensions to execute a batch file in invisible mode.

  • Create a plain text file and name it <scriptname>.vbs

  • Paste the following code in the .vbs file

      Set WshShell = CreateObject("WScript.Shell") 
      WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
      Set WshShell = Nothing
    
  • Change the name and path of you batch file according to your need

  • Save the .vbs file and schedule the same in schtasks

Trodden answered 2/10, 2010 at 15:51 Comment(2)
Thanks, I'll try this. But I still wonder if it is impossible to do with a batch file...Unseal
This does not answer OP's question. The proper solution is in the answer below from @mpod.Bachelor

© 2022 - 2024 — McMap. All rights reserved.