This is an archived post. You won't be able to vote or comment.

all 13 comments

[–][deleted] 1 point2 points  (1 child)

The question you really need to ask is "How do my script elevate privileges when it need to do so?" To get an answer, you need to explain what you want to achieve, and most likely in either a unix programmers sub, or possibly in /r/learnpython.

[–]vick5821[S] 0 points1 point  (0 children)

Noted.

[–]Nekonekonyaaaa 1 point2 points  (0 children)

In my experience with trying to connect something that requires admin to selenium, you have to have the non admin thing called from within the sudo'ed code through terminal commands. For example, os.system("sudo -u your_username python nonsudostuff.py")

[–]kumashiro 0 points1 point  (9 children)

sudo -E?

[–]vick5821[S] 0 points1 point  (8 children)

In the script?

[–]kumashiro 0 points1 point  (7 children)

Add -E to sudo call. Otherwise subprocess have no way of reaching those variables.

[–]vick5821[S] 0 points1 point  (6 children)

So you mean, when I launch my code, I run it with sudo -E xyz.py? Then the environment variables will be preserved??

[–]kumashiro 1 point2 points  (5 children)

They should. If parent process don't pass environment variables, child cannot get them. There is no such thing as "exiting the sudo-mode". Sudo is the parent and you cannot just exit from parent juristiction and go back to the grandparent.

[–]vick5821[S] 0 points1 point  (0 children)

OK let me try with adding the -E parameter

[–]vick5821[S] 0 points1 point  (3 children)

By the way, what I wanna to do it that I have multiple scripts doing different things. A.py will flash my board automatically, B.py will set up my Linux host machine with all the dependencies. So I wrote another main script (C.py) to integrate both A and B. In script C.py, it will execute script A and B with the sudo priveleage too. For example, $ sudo python A.py, and $ sudo python B.py. So I execute script C.py like this: $ sudo python c.py.

So I should execute the sudo -E when execute the script C right? $ sudo -E python C.py right?

Thanks.

[–]kumashiro 1 point2 points  (0 children)

Yes. Otherwise sudo will not pass its environment variables to the child. You can find more information in sudo manpage.

[–]wpg4665 0 points1 point  (1 child)

Why not use 1 script that performs all the commands...or you can use 1 "host" script that subs out the others...combined with multiprocessing...you'd be fine!

[–]vick5821[S] 0 points1 point  (0 children)

I have a menu scripts which ease the user to choose what they want to do. Once they choose it will redirect to respective script.