Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 605
Misha Moroshko Avatar asked Oct 02 '10 15:10

Misha Moroshko


People also ask

How do you Run a scheduled task without a command window appearing?

Right-click the Task Scheduler Library folder. Click the Create Task option. In the “General” tab, under the “Security options” section, select the Run whether user is logged on or not option. (This is the option that will make the command window not to appear when the task runs automatically.)

How do I Run a scheduled task from the command line?

Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to change the time to run the task 9:00am and press Enter:Syntax SCHTASKS /CHANGE /TN "FOLDERPATH\TASKNAME" /ST HH:MM Example SCHTASKS /CHANGE /TN "MyTasks\Notepad task" /ST 09:00.

How do you make a scheduled task Run in the background?

By default, when you create a new scheduled task, the task is set to Run only when user is logged on. As a result of that setting the scheduled task will run in the foreground. 2. If you want to run the scheduled task in background (hidden), then select the Run whether user is logged on or not option and click OK.


2 Answers

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"
                 ....
like image 103
pmod Avatar answered Oct 01 '22 12:10

pmod


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

like image 39
Rutesh Makhijani Avatar answered Oct 01 '22 12:10

Rutesh Makhijani