How can I write script output if it is run by the task scheduler?

Using Windows Server 2008, how do I capture output script that runs with the Windows Task Scheduler?

I am testing a rather long custom print script package, and for debugging purposes I would like to see all the output from it every night.

+53
scheduled-tasks batch-file capture
Dec 28 2018-11-28T00:
source share
7 answers

You may have debug.cmd that calls yourscript.cmd

yourscript.cmd > logall.txt 

you are planning debug.cmd instead of yourscript.cmd

+28
Dec 28 2018-11-12T00:
source share

Try this as a command line in the task scheduler:

 cmd /c yourscript.cmd > logall.txt 
+53
Sep 03 '13 at 23:29
source share

With stderr (where most of the errors go):

 cmd /c yourscript.cmd > logall.txt 2>&1 
+34
May 22 '14 at 13:25
source share

>> will add a log file, not overwrite it every time. 2>&1 also send errors to your log file.

 cmd /c YourProgram.exe >> log.txt 2>&1 
+23
May 25 '16 at 14:23
source share

To add @ user2744787 comment, here is a screenshot showing how to use cmd with arguments in a scheduled task:

enter image description here

+11
Mar 23 '17 at 3:47 on
source share

You can write the lines you want to output to the log file as follows:

 @echo off echo Debugging started >C:\logfile.txt echo More stuff echo Debugging stuff >>C:\logfile.txt echo Hope this helps! >>C:\logfile.txt 

Thus, you can choose which commands to print, if you do not want to spend everything, just get what you need to see. > will output it to the specified file (by creating the file if it does not exist and overwrite it if it does). >> will be added to the specified file (by creating the file if it does not exist, but added to the content if it does).

+6
Dec 29 '11 at 9:26 a.m.
source share

Try cmd / c and custom timestamp

To build on the answers of others here, it may be that the output file you want to create has a date or time embedded in the name of the output file. With CMD.exe this can be achieved as follows. Note that this method prints the string output of the internal Windows environment variables and cuts them based on the position of the character, so it may be wrong for the Windows area that you are using. This method is limited to working with one area of ​​Windows. For example, what works for the United States may not be successful for the United Kingdom due to differences in how the date and / or time is formatted for each region. PowerShell more efficient in that it can work with different areas of Windows, and PowerShell can call CMD.exe to run .cmd files, but this is beyond the scope of this answer.

Using CMD.exe

You can access the various components of date and time by editing the internal environment variables %date% and %time% as follows (again, the exact cutoff values ​​depend on the region configured in Windows):

  • Year: %date:~10,4%
  • Month: %date:~4,2%
  • Day: %date:~7,2%
  • Hour: %time:~0,2%
  • Minute: %time:~3,2%
  • Second: %time:~6,2%

Suppose you want your log file to be named " Log_[yyyyMMdd]_[hhmmss].txt ", you should use the following:

 Log_%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt 

To verify this, run the following command line:

 cmd /c echo Log_%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt 

Combining everything together to redirect both stdout and stderr from your script to a log file with the current date and time, use the following command as a command line:

 cmd /c YourProgram.cmd > Log_%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt 2>&1 

EDIT: Warning: the timestamp may contain a space!

As @ user25064 pointed out, sometimes times before noon can appear with the leading space in the hourly part of the time. Therefore, the file name must be enclosed in quotation marks to exclude the possibility of disconnecting the computer from the shell as follows:

 cmd /c YourProgram.cmd > "Log_%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt" 2>&1 
+4
Aug 22 '17 at 20:55 on
source share



All Articles