如果
ddrescue将其stdout / stderr重定向到管道时仍不更改其输出,则可以使用
tee实用程序在终端上显示输出并将其保存到文件中:
$ ddrescue input_path output_path ddrescue_logfile |& tee logfile
如果确实如此,那么您可以尝试使用
script实用程序提供一个伪tty :
$ script -c 'ddrescue input_path output_path ddrescue_logfile' -q logfile
如果它直接写入终端,则可以
screen用来捕获输出:
$ screen -L -- ddrescue input_path output_path ddrescue_logfile
screenlog.0默认情况下,输出保存在文件中。
要
tee在Python中模拟基于命令的命令而不调用
tee实用程序:
#!/usr/bin/env python3import shleximport sysfrom subprocess import Popen, PIPE, STDOUTcommand = 'ddrescue input_path output_path ddrescue_logfile'with Popen(shlex.split(command), stdout=PIPE, stderr=STDOUT, bufsize=1) as p: with open('logfile', 'wb') as logfile: for line in p.stdout: logfile.write(line) sys.stdout.buffer.write(line) sys.stdout.buffer.flush()tee使用
shell=True以下命令在Python中调用基于命令:
#!/usr/bin/env pythonfrom pipes import quotefrom subprocess import callfiles = input_path, output_path, ddrescue_logfilerc = call('ddrescue {} | tee -a drclog'.format(' '.join(map(quote, files))), shell=True)要模拟
script基于命令:
#!/usr/bin/env python3import osimport shleximport ptylogfile = open('logfile', 'wb')def read(fd): data = os.read(fd, 1024) # doesn't block, it may return less logfile.write(data) # it can block but usually not for long return datacommand = 'ddrescue input_path output_path ddrescue_logfile'status = pty.spawn(shlex.split(command), read)logfile.close()要
screen在Python中调用命令:
#!/usr/bin/env python3import osimport shlexfrom subprocess import check_callscreen_cmd = 'screen -L -- ddrescue input_path output_path ddrescue_logfile'check_call(shlex.split(screen_cmd))os.replace('screenlog.0', 'logfile')


