I'm an absolute raspberry pi / phyton newbie so be patient with me.

Now for the problem (problem? more like a question). (I want to apologize before hand if such a request was already answered before, I just could not find something to useful.)

I have set up a raspberry pi rev2.1 with a mumble server which explicitely does not START on pi boot. I want to start / stop the mumble server service after the pi has booted and stabilized itself, manually via button presses. Also incase of a problem with the mumble service, I want to stop it via button press and start it again.

I have read some tutorials and codes now for reboot and shutdown button phyton scripts and I think my request is rather similar.

Here is the code I clobbered together so far. (Don't hit me to hard pls, this is my first phyton script..?)

#!/usr/bin/env python

# import required modules
import time
import RPi.GPIO as GPIO

# main function
def main():

    # use GPIO pin numbering convention

    # set up variable and GPIO pin for input
    MumbleStart = 2
    GPIO.setup(MumbleStart, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    MumbleStop = 3
    GPIO.setup(MumbleStop, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    while True:
        # button MumbleStart pressed, starting service
        if not GPIO.input(MumbleStart):
            sudo /etc/init.d/mumble-server start

        # button MumbleStop pressed, stoping service
        if not GPIO.input(MumbleStop):
            sudo /etc/init.d/mumble-server stop

        # wait 200ms


if __name__ == '__main__':

# call main function

Now I'm not certain if I can actually use it that way. I believe that the code for calling up the mumble-server, should be something else. But I can't find an explicit example of calling up a service via a phyton script. So if anybody has an idea, please help me.

  • 1
    Your right that is not how to call a shell command from python. This question stackoverflow.com/questions/89228/… should give you the details. Second when you type a : in python the following lines need to be indented 4 spaces. I am going to fix that part of your code in your question - since it is easier than explaining it. This question will help with the sudo portion of the command stackoverflow.com/questions/13045593/…. Sep 2 '16 at 10:27
  • Sorry for my late reply. First thank you for your answer. Ok, now that brings me closer to my goal. This with : the spaces march in after, I didn't know. I saw it in some tutorials but didn't realize it would be important or needed. Again thank you.
    – TarAnTani
    Sep 5 '16 at 8:57

To call a process use the subprocess python module. In your example this would be:

subprocess.call(["/etc/init.d/mumble-server", "start"])


subprocess.call(["/etc/init.d/mumble-server", "stop"])

Note that I have left off sudo, running sudo in a script like this would prompt for a password, it is better to run the entire script as root as and avoid using sudo.

An aside: this may be a copy paste error into stack overflow but the end of your script:

if __name__ == '__main__':

# call main function

Should be:

if __name__ == '__main__':
    # call main function

Note: Normally it is more secure to run the script as a normal user and only elevate to root when needed but given what your script is doing setting this up might be more effort than it is worth to set it up (if you are interested look at setting up passwordless sudo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.