1

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.

Rajs123
  • 663
  • 13
  • 30
  • 1
    Why does bash history not store the commands ? – 123 Apr 21 '16 at 08:44
  • Also the question you linked only saves the output not the command that has been typed – 123 Apr 21 '16 at 08:53
  • @123 Maybe OP are thinking about the local `.bash_history` and are not the remote? – Andreas Louv Apr 21 '16 at 09:13
  • I am sorry .bash_history stores the command on exit which was why I was not able to find in .bash_history. I will update the question. Is there anyway I can get the output of commands executed? – Rajs123 Apr 21 '16 at 09:28

1 Answers1

1

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.

alamar
  • 18,729
  • 4
  • 64
  • 97