How to store all command outputs and errors

bashcapturecommand linepython

Hi there!

I need to capture all command, stdout and stderr without any addition to the commands.
I mean, that I have to save/store the output of these:

  1. $ echo 'test'
  2. $ ls -l
  3. $ cat some_file

commands.file

  1. alex@bender:/pwd$ echo 'test'
  2. alex@bender:/pwd$ ls
  3. alex@bender:/pwd$ cat some_file
  4. alex@bender:/pwd$ fsdfsdfsd

stdout.file

  1. test
  2. test.txt some_file
  3. some file content

stderr.file

_4. fsdfsdfsd

as for first part of question, I found script, which use a trap and before evaluating the command it stores the command to log.
But what about storing output?
repeat: this should be solved without any additions to command.
I mean, after running script, I dont need to write, for example first command
ls > log.file I want see it without ">log.file"

I write some python script, it works like command wrapper.

if name == 'main':
user = os.getlogin()
machine_name = os.uname()[1]

stdinbuff = open('commands', 'w')                                             
stdoutbuff = open('outs', 'w')                                                

command_string = 1                                                            
while command_string != '0':                                                  

    a = user + '@' + machine_name + ":" + os.getcwd() + '>';                  
    a = a.replace('/home/', '~/')                                             
    print a,                                                                  
    command_string = raw_input()                                              
    command_args = command_string.split()                                     

    proc = subprocess.Popen(command_args, stdout=subprocess.PIPE)             
    (out, err) = proc.communicate()                                           
    print out                                                                 

    stdinbuff.write(command_string+'\n')                                      
    stdoutbuff.write(out+'\n')                                                

stdinbuff.close()                                                                 
stdoutbuff.close()

but in this wrapper, I can't use autocomplete, like in bash, and this script can't store all what I want.

Maybe, someone could tell me, what I have to do?

Best Answer

Maybe the screen tool can do the trick for you with the screen logging function. Just simply start screen in commandline and with the keys CTRL-a + H (first Combination of CTRL + a and then a capital H with SHIFT + H, just to clarify) everything will be logged in the file screenlog.0. The directory where the logfile is found depends on where you started screen, i. e. if you started screen in your $HOME then the logfile will be found there.

Related Question