136

Using Windows Server 2008, how do I go about capturing the output of a script that is being ran with the windows task scheduler?

I'm testing a rather long custom printing batch-script, and for debugging purposes, I would like to see all of the output from it every night.

Ivan
  • 9,089
  • 4
  • 61
  • 74
Anthony Miller
  • 15,101
  • 28
  • 69
  • 98
  • 2
    Sometimes, this will not work i.e. no log file is generated. One scenario that can lead to this is when the task did not start at all, because Windows mistakenly thinks that it is still running. You can track that by loking at task history (history tab in task property - the information may take a few seconds to show up) – Daniel May 04 '17 at 06:21

10 Answers10

119

Try this as the command string in Task Scheduler:

cmd /c yourscript.cmd > logall.txt
Igor
  • 33,276
  • 14
  • 79
  • 112
user2744787
  • 1,206
  • 2
  • 9
  • 2
  • 13
    Additionally, use `cmd /c "path with spaces/command.cmd > file.txt"` if you have spaces. The `2>&1` from [Ivan's answer](https://stackoverflow.com/a/23808212/3136474) also goes inside the quotes. – Dinei May 24 '17 at 15:54
  • 6
    need to use double quotes to get mine to work: https://stackoverflow.com/a/6378038/1747983 `cmd /c ""C:\temp\My test dir\something 123\myTool.exe" > Tilo_log.txt 2>&1"` – Tilo Feb 04 '19 at 17:59
  • Now I have a command window showing up when running the task. How to prevent that? – Chris Apr 30 '20 at 22:47
  • @Chris check out this answer: https://stackoverflow.com/a/52206695/184839 – SWilk Sep 21 '20 at 09:42
  • You'll miss errors without redirecting stderr by adding `2>&1` as stated in another answer on this page https://stackoverflow.com/a/23808212/727345 – JonoB Jun 04 '21 at 08:43
  • Note, that using single `>` will overwrite the file the instant another execution of the task **starts**, which depending on the frequency and duration may mean right away after completing the previous execution. Consider using `>>` to append to the log file instead, maybe implement some basic log rolling or at least rename the previous log file before overwriting it. – Krzysztof Jabłoński Jun 15 '23 at 21:34
83

To supplement @user2744787's answer, here is a screenshot to show how to use cmd with arguments in a Scheduled Task:

Scheduled Task cmd output to logfile

Program/script: cmd

Add arguments: /c run_with_default_port.bat > IMQuantWebServices.log

mwfearnley
  • 3,303
  • 2
  • 34
  • 35
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
  • 6
    Seems you are starting a service via a scheduled task. If this should be a service running persistently in the background, have a look at NSSM as an alternative. It runs any command as a windows service and handles restarting after failure etc. – leemes Jun 23 '17 at 15:37
  • weow, finally. this is the exact procedure to schedule a cmd job. – Janatbek Orozaly Jul 14 '21 at 04:57
78

With stderr (where most of the errors go to):

cmd /c yourscript.cmd > logall.txt 2>&1
Ivan
  • 9,089
  • 4
  • 61
  • 74
  • This improves on the accepted answer for the OP's request to log for debugging purposes because it will log errors too. – JonoB Jun 04 '21 at 08:45
57

The >> will append the log file, rather than overwriting it each time. The 2>&1 will also send errors to your log file.

cmd /c YourProgram.exe >> log.txt 2>&1
WhiteHotLoveTiger
  • 2,088
  • 3
  • 30
  • 41
41

You can have a debug.cmd that calls yourscript.cmd

yourscript.cmd > logall.txt

you schedule debug.cmd instead of yourscript.cmd

rene
  • 41,474
  • 78
  • 114
  • 152
18

Use the cmd.exe command processor to build a timestamped file name to log your scheduled task's output

To build upon answers by others here, it may be that you want to create an output file that has the date and/or time embedded in the name of the file. You can use the cmd.exe command processor to do this for you.

Note: This technique takes the string output of internal Windows environment variables and slices them up based on character position. Because of this, the exact values supplied in the examples below may not be correct for the region of Windows you use. Also, with some regional settings, some components of the date or time may introduce a space into the constructed file name when their value is less than 10. To mitigate this issue, surround your file name with quotes so that any unintended spaces in the file name won't break the command-line you're constructing. Experiment and find what works best for your situation.

Be aware that PowerShell is more powerful than cmd.exe. One way it is more powerful is that it can deal with different Windows regions. But this answer is about solving this issue using cmd.exe, not PowerShell, so we continue.

Using cmd.exe

You can access different components of the date and time by slicing the internal environment variables %date% and %time%, as follows (again, the exact slicing values are dependent on the region configured in Windows):

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

Suppose you want your log file to be named using this date/time format: "Log_[yyyyMMdd]_[hhmmss].txt". You'd use the following:

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

To test this, run the following command line:

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

Putting it all together, to redirect both stdout and stderr from your script to a log file named with the current date and time, use might use the following as your 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

Note the use of quotes around the file name to handle instances a date or time component may introduce a space character.

In my case, if the current date/time were 10/05/2017 9:05:34 AM, the above command-line would produce the following:

cmd /c YourProgram.cmd > "Log_20171005_ 90534.txt" 2>&1
Community
  • 1
  • 1
STLDev
  • 5,950
  • 25
  • 36
9

You can write to a log file on the lines that you want to output like this:

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

This way you can choose which commands to output if you don't want to trawl through everything, just get what you need to see. The > will output it to the file specified (creating the file if it doesn't exist and overwriting it if it does). The >> will append to the file specified (creating the file if it doesn't exist but appending to the contents if it does).

Bali C
  • 30,582
  • 35
  • 123
  • 152
7

Combining the other answers, which each had some useful tips, but none of them captured every nuance:

In the Task Scheduler > Actions:

Action: Start a program

Program/script: cmd

Add arguments: /c ""C:\my path\my task.cmd" >> "C:\my path\my task.log" 2>&1"

The above example demonstrates these features:

  • Allows spaces in the path/filename. If there are no spaces in the path, then you can eliminate the nested quotes: /c "C:\path\task.cmd >> C:\path\task.log 2>&1"

  • Appends to the log. If you want to overwrite the log each time, then use > instead of >>

  • Includes both STDOUT and STDERR in the log. If you only want STDOUT, then remove 2>&1

wisbucky
  • 33,218
  • 10
  • 150
  • 101
4

Example how to run program and write stdout and stderr to file with timestamp:

cmd /c ""C:\Program Files (x86)\program.exe" -param fooo >> "c:\dir space\Log_%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%.txt" 2>&1"

Key part is to double quote whole part behind cmd /c and inside it use double quotes as usual. Also note that date is locale dependent, this example works using US locale.

David Navrkal
  • 464
  • 5
  • 8
2

This snippet uses wmic.exe to build the date string. It isn't mangled by locale settings

rem DATE as YYYY-MM-DD via WMIC.EXE
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I
set RDATE=%datetime:~0,4%-%datetime:~4,2%-%datetime:~6,2% 
jim birch
  • 413
  • 4
  • 8