Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Use sudo to re-launch a Python script as root
# This is how I ensure a Python script is launched as root, and automatically
# call 'sudo' to re-launch it as root if not.
# I found it useful to check the parameters are valid *before* re-launching as
# root, so I don’t have to enter the sudo password if there is a problem with
# the parameters, or I just want the help message.
import os
import sys
# At this point we may be running as root or as another user
# - Check the parameters are valid - show an error if not
# - Show the help message if requested
# Don't do any work or anything time-consuming here as it will run twice
if os.geteuid() != 0:
# os.execvp() replaces the running process, rather than launching a child
# process, so there's no need to exit afterwards. The extra "sudo" in the
# second parameter is required because Python doesn't automatically set $0
# in the new process.
os.execvp("sudo", ["sudo"] + sys.argv)
# Now we are definitely running as root
# - Make the changes to the system settings (e.g. Apache config)

This comment has been minimized.

Copy link

tuxmann commented Dec 8, 2016

I had a problem running this in Linux Mint 18. I would receive a "command not found" until I changed the final line to to the following. Thank you for the well documented code. It was educational.

os.execvp("sudo", ["sudo"] + ["python"] + sys.argv)


This comment has been minimized.

Copy link

flash76 commented Sep 11, 2017

Or you can create another script. In the second script write all the sudo commands and in the first, say sudo . <filename>.sh


This comment has been minimized.

Copy link

glapadre commented Mar 28, 2019

I've also been trying to do something very similar on my Raspberry Pi 3 that's connected to a LED Matrix...

I have a function within a script that restarts the script at certain times throughout the day to change the brightness level on the LED board (basically turning the brightness down at night).. I'll post the function below.

""" Function to restart the script 3 times throughout the day to change
    brightness settings of the LED Matrix.
    1. At 8:00am go to full brightness (--led-brightness=100)
    2. At 7:00pm go to mid brightness (--led-brightness=50)
    3. At 10:00pm go to low brightness (--led-brightness=30)
    4. At 11:00pm go to nightime mode (--led-brightness=5)
def restart_script():
    currentDT =
    time = currentDT.strftime("%I:%M%p")
    if(os.geteuid() != 0):
        if (time == "08:00AM"):
            os.execvp("sudo", ['sudo', './', '--led-brightness=100', '--led-pwm-lsb-nanoseconds=300'])
        elif (time == "10:00PM"):
            os.execvp("sudo", ['sudo', './', '--led-brightness=10', '--led-pwm-lsb-nanoseconds=300'])

Don't mind '--led-brightness=5' and '--led-pwm-lsb-nanoseconds=300' they're just the sys.argv's...
The problem I'm running into is I don't know the master password for 'daemon' ... Does anyone know a way around this?


We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for daemon: 
Sorry, try again.
[sudo] password for daemon: 
Sorry, try again.
[sudo] password for daemon: 
Sorry, try again.
sudo: 3 incorrect password attempts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.