简体   繁体   中英

Redirect output of command to file and stdout [Only once, not at each command]

I want to have a snapshot of all the commands I executed and output (stdout, stderr) in a file to view later. Often I need to view it for debugging purposes.

This link gives the output for single command: How to redirect output to a file and stdout

However, I do not want to write it for each command since it is time consuming. Also, this does not log the command I executed.

Example functionality:

bash> start-logging logger.txt
bash> echo "Hi"
Hi
bash> cat 1.txt
Contents of 1.txt
bash> stop-logging

Contents of logger.txt

bash> echo "Hi"
Hi
bash> cat 1.txt
Contents of 1.txt

Ideally, I want the above behavior. Any other command with some missing functionality (maybe missing command executed from tmp.txt) would also help.

You can use

$ bash | tee log

to start logging and then

$ ^D

(that's Ctrl-D)

to stop logging.

Create a new bash session that will log, then terminate it when it's not needed.

But unfortunately, bash seems to not write its prompt and commands to stderr so you will only catch command output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM