amoffat / sh

Python process launching
https://sh.readthedocs.io/en/latest/
MIT License
6.98k stars 506 forks source link

RFH: Unable to pipe `sudo` process to `sudo` process #714

Closed jamincollins closed 10 months ago

jamincollins commented 11 months ago

I can't seem to find a way to handle piping a sudo process to another sudo process, as both need to ask for a password.

For example:

    sh.contrib.sudo("dd", "bs=4M", f"of=/dev/{lvm_group}/{name}",
        _in=sh.contrib.sudo("rbd", "export", "--no-progress", volume, "-", _piped=True)
    )

I've also tried running the entire script as root with the following, but it never seems to make progress.

    sh.dd("bs=4M", f"of=/dev/{lvm_group}/{name}",
        _in=sh.rbd("export", "--no-progress", volume, "-", _piped=True)
    )

For now, I'm placing the above logic in a separate shell script that sh executes.

jamincollins commented 11 months ago

Bonus points would be a way to allow progress updates from the subprocess to display.

amoffat commented 11 months ago

Try using sh.sudo directly:

sh.sudo.dd("bs=4M", f"of=/dev/{lvm_group}/{name}",
    _in=sh.sudo.rbd("export", "--no-progress", volume, "-", _piped=True)
)

The sh.contrib.sudo will always ask for a password every time it is executed.

but it never seems to make progress.

Are you sure it isn't just slow? Also, if you know progress is printed on stderr, you can redirect the output to sys.stderr or a callback.

If it is progressing but is just slow, there may be some more switches we can flip to make it faster. Let me know.

ecederstrand commented 10 months ago

Closing. Feel free to reopen if you have time to get back to this issue.