1

I have a problem with /home folder as root. Here is my script:

home = os.path.expanduser("~")
print home             # home is good: /home/guillaume

# gksudo
euid = os.geteuid()
if euid != 0:
    print "Running sudo.."
    args = ['gksudo', sys.executable] + sys.argv + [os.environ]
    os.execlpe('gksudo', *args)
print home            # home has changed: /root

How can I get the good /home after gksudo has run ? Thanks

Edit: I've tried

subprocess.call(['gksudo', 'script.py'])

The script is launched, but the operations inside the script are not in root

Guillaume
  • 2,752
  • 5
  • 27
  • 42

1 Answers1

3

sudo sets a number of environment variables for the process; see man sudo for a list. In this case, you can use SUDO_USER:

# from under sudo
home = os.path.expanduser(os.environ["SUDO_USER"])
hamstergene
  • 24,039
  • 5
  • 57
  • 72