Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm using a command line script to adjust multiple settings on a computer. However, I would like to have the entire output to also be logged into a .txt or .log file.

When I use my basic knowledge of the logging system it will put the output in a file but not actually execute it. In my case I would need it executed and then logged into a file for later reference.

Would someone be able to tell me how to do this?

Thanks in advance! Dempsey

share|improve this question

migrated from serverfault.com Jan 8 at 3:52

This question came from our site for professional system and network administrators.

    
You haven't even specified an operating system! –  Michael Hampton Jan 5 at 23:50

2 Answers 2

This script will execute ls and log its output to a file called log.txt:

exec >log.txt 2>&1
ls

The log will not be executed.

share|improve this answer
    
Note that this assumes Unix/Linux, but my guess is the OP talks about Windows. It's also not running a script, but logging the output of the commands you enter and omits the critical information of how to stop the madness ... –  SvW Jan 4 at 12:30
    
I'm indeed talking about Windows. Thanks though :) –  Dempsey FoxDie Van Assche Jan 4 at 12:33

In your question, you mention:

"... it will put the output in a file but not actually execute it. In my case I would need it executed and then logged into a file for later reference."

Since you say the program is running and its output is being put into the file, I thought that you might have meant "displayed", instead of "executed".

If that is not what you meant, then it would have probably helped if that was explained better, perhaps with some sample output.

In any case, I'm posting this answer in case there are others who find this question/answer helpful.

So, basically, it sounds like you want the output of a script to be captured to a file, and to also be able to see the output of the script on the screen while the script is running.

(tl;dr version: use wintee, like this:

script 2>&1 | wtee logfile.txt)


For this post, I will be using a small test batch file, but your script could be as big and complicated or as simple as you need:

C:\>type a.cmd
@echo off

echo Command: "dir /b a*"
dir /b a*
echo.

echo Command: "dir /b non-existant-file"
dir /b non-existant-file
echo.


This is what happens when I run this batch script:

C:\>a.cmd
Command: "dir /b a*"
a.cmd

Command: "dir /b non-existant-file"
File Not Found

Notice that in the test script, the first execution of the "dir" command is successful, and the second one fails. I do this only to show what will happen with "error messages" when you run your script.

If I run the script and use redirection (">") to capture the output, I will see this

C:\>a.cmd > log.txt
File Not Found

C:\>type log.txt
Command: "dir /b a*"
a.cmd

Command: "dir /b non-existant-file"

C:\>

Notice the error message "File Not Found" was shown on the screen when the script was run, and was not actually captured into the file. That's because ">" captures "normal output" which has been sent to the STDOUT stream. "Error messages" are normally sent to the STDERR stream.

To capture "normal output" and "error messages", you need to also capture the STDERR stream, which is indicated by the "2" in "2>&1" in the command here:

C:\>a.cmd > log.txt 2>&1
C:\>type log.txt
Command: "dir /b a*"
a.cmd

Command: "dir /b non-existant-file"
File Not Found

In unix, there is a standard command: "tee"

Using the "tee" command, you can capture output from a program, and also display the output to the screen, at the same time.

The "tee" command is not standard with windows, but you can download a free version of "tee" for windows here: wintee. The downloaded program is named: "wtee.exe".

You use the "wtee.exe" program as shown below.

This will capture the script output to the file named "log.txt" as before, and it will also display the output to the screen while the script is running:

C:\>a.cmd 2>&1 | wtee log.txt
Command: "dir /b a*"
a.cmd

Command: "dir /b non-existant-file"
File Not Found

C:\>type log.txt
Command: "dir /b a*"
a.cmd

Command: "dir /b non-existant-file"
File Not Found
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.