I have a python program that includes a GUI. What I want to do is check if running as root, if not running as root then escalate to root priveledges. So far, the code below seems to work, i'm just not sure if that is the best way.
My question is, is this the best way to escalate to root privileges? I can't just rely on someone to run it from a terminal with sudo as someone might doubleclick the .py file.
Code:
if not os.geteuid()==0: ## Check if running as root
if not os.isatty(0): ## If not running in a terminal
os.system("gksudo savfl-sst.py") # script is restarted, now as root
else: ## If we are in a terminal
sys.exit("\nPlease run this utility as root\n")
:EDIT:
I just wanted to add that doing an os.system that runs the same py (this code is from savfl-sst.py) without stopping the original process just feels wrong.
Bookmarks