elevate
elevate copied to clipboard
Line-buffered stdout logging of elevated process does not appear to work (osascript issue)
Hello,
I am writing a python desktop GUI application and I wanted to embed a small installer into it. The idea is, when the user clicks on the install button, the GUI application will call itself with a special "run installer" argument. Then instead of launching another copy of the GUI, it would call elevate and then execute the installer script:
if "run_macos_background_service_installer" in sys.argv:
elevate(graphical=True)
if os.getuid() != 0:
print(f"elevate() failed: not running as root. uid={os.getuid()}")
sys.exit(1)
# bufsize=1 means line-buffered.. ? https://docs.python.org/3/library/subprocess.html#popen-constructor
proccess = subprocess.Popen(["bash", app_context.get_resource('install_daemon.sh')],
bufsize=1, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(proccess.stdout.readline, b''):
print(line.rstrip().decode("utf-8"))
sys.stdout.flush()
proccess.wait(10)
sys.exit(proccess.returncode)
else:
... normal app stuff...
I noticed that posix.py
will do nothing if its already running as root:
https://github.com/barneygale/elevate/blob/master/elevate/posix.py#L25-L27
I also noticed that if I run this app as a normal user, the logs from install_daemon.sh
wont appear until after the process exits. However if I run the app as root (causing elevate
to exit early instead of calling os.execlp(...)
) then the logs appear in real time. So far I have only tested in Mac OS. If you wish to see the full example my app is here, sorry about the messy code:
https://git.sequentialread.com/forest/greenhouse-desktop/src/commit/aae851dcbac899360c449ab1e24594197c34582b
For now this issue is not going to block me, I will simply output to a temp file and then poll that file instead of doing stdout pipe.
Never mind, it looks like this might be a limitation of osascript, not an elevate issue. See: https://stackoverflow.com/questions/49171769/how-where-to-best-retrieve-sudo-password-via-a-native-gui-on-a-macos-python-ba
For now I have solved this by writing to a temp file and then polling it.
However if it would be possible to have an option for tell terminal to run xyz
or whatever where it would actually do the line-by-line piping (without actually opening a terminal window), that might be cool. I never tried it.