Skip to content

Instantly share code, notes, and snippets.

@hikoz
Created December 15, 2010 04:45
Show Gist options
  • Save hikoz/741643 to your computer and use it in GitHub Desktop.
Save hikoz/741643 to your computer and use it in GitHub Desktop.
dd with progress in python
#!/usr/bin/env python
import sys
import time
import signal
from subprocess import Popen, PIPE
dd = Popen(['dd'] + sys.argv[1:], stderr=PIPE)
while dd.poll() is None:
time.sleep(.3)
dd.send_signal(signal.SIGUSR1)
while 1:
l = dd.stderr.readline()
if 'records in' in l:
print l[:l.index('+')], 'records',
if 'bytes' in l:
print l.strip(), '\r',
break
print dd.stderr.read(),
@hlmtre
Copy link

hlmtre commented Nov 23, 2015

This rocks, thanks.

@Lampe2020
Copy link

Lampe2020 commented Feb 9, 2023

As a Python3 module:

#!/usr/bin/env python3

import time, signal, subprocess

def run_dd(*args):
    cmd = ['dd', *args]
    dd = subprocess.Popen(cmd, stderr=subprocess.PIPE)
    while dd.poll() is None:
        time.sleep(.3)
        dd.send_signal(signal.SIGUSR1)
        while True:
            l = dd.stderr.readline()
            if b'records in' in l:
                print(l[:l.index('+')], 'records')
            if b'bytes' in l:
                print(l.strip(), '\r')
                break
    print(dd.stderr.read())

if __name__ == '__main__':
    from sys import argv
    run_dd(*argv[1:])

Usage:

import dd
dd.run_dd('if=/dev/sda', 'of=/dev/sdb', 'status=progress')

The code seems to hang on the line with stderr.readline.

@twirrim
Copy link

twirrim commented Apr 26, 2023

As dd doesn't seem to give any output (neither on stderr nor stdout) except when it's complete there's not much point in this (anymore?).

dd won't give you any progress output unless you give it an explicit status=progress option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment