lwhistler
Posts: 16
Joined: Sat Jul 16, 2016 10:37 pm

How can the raspberry be shutdown with a python script and no sudo?

Mon Apr 23, 2018 10:33 pm

How can the raspberry be shutdown with a python script and no sudo?

klricks
Posts: 7159
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: How can the raspberry be shutdown with a python script and no sudo?

Mon Apr 23, 2018 11:48 pm

lwhistler wrote:
Mon Apr 23, 2018 10:33 pm
How can the raspberry be shutdown with a python script and no sudo?
If you are running as pi user then sudo is not required to shutdown or reboot. Works with or without sudo.
Use a subprocess to execute bash commands. Not the only way.....

Shutdown without sudo:

Code: Select all

#!/usr/bin/python
import subprocess
subprocess.Popen(['shutdown','-h','now'])
This is reboot with sudo:

Code: Select all

#!/usr/bin/python
import subprocess
subprocess.Popen(['sudo','shutdown','-r','now'])
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

pcmanbob
Posts: 9497
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: How can the raspberry be shutdown with a python script and no sudo?

Tue Apr 24, 2018 10:07 am

klricks wrote:
Shutdown without sudo:

Code: Select all

#!/usr/bin/python
import subprocess
subprocess.Popen(['shutdown','-h','now'])
just tried this on stretch light using the pi user and it does not work
you get this error message

Code: Select all

Failed to set wall message, ignoring: The name org.freedesktop.PolicyKit1 was not provided by any .service files
Failed to power off system via logind: The name org.freedesktop.PolicyKit1 was not provided by any .service files
Failed to open /dev/initctl: Permission denied
Failed to talk to init daemon.

just as I suspected you would because you always need sudo to run shutdown.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

hippy
Posts: 7783
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How can the raspberry be shutdown with a python script and no sudo?

Tue Apr 24, 2018 11:43 am

Looking in the Python program which I run without sudo I have ...

Code: Select all

while True:
  if GPIO.input(GPIO_BUTTON) == 0:
    result = os.popen("sudo shutdown -h now").read()
    return
  time.sleep(0.1)

User avatar
DougieLawson
Posts: 39182
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How can the raspberry be shutdown with a python script and no sudo?

Tue Apr 24, 2018 11:49 am

That program uses sudo.

It's very difficult to shutdown a Linux system without using sudo (or a setuid program owned by root) as the trigger for the systemd init program to run an orderly shutdown needs elevated privileges.

You can add a line to /etc/sudoers

Code: Select all

pi raspberrypi =NOPASSWD: /usr/bin/systemctl poweroff,/usr/bin/systemctl halt,/usr/bin/systemctl reboot
That's assuming a userid=pi on a hostname=raspberrypi system (change as appropriate).
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

jbudd
Posts: 1429
Joined: Mon Dec 16, 2013 10:23 am

Re: How can the raspberry be shutdown with a python script and no sudo?

Tue Apr 24, 2018 12:00 pm

You can do it with a root owned setuid executable.

But it has to be a compiled program - setuid is ignored by shell scripts (and Python, I guess)

Code: Select all

/*sd.c  C Program to Shutdown or Turn Off the Computer in Linux.*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
  system("shutdown -P now");
  return 0;
}

Code: Select all

gcc -o sd sd.c
sudo chown root sd
sudo chgrp root sd
sudo chmod 6755 sd

#And to try it:
./sd
The C source is based on https://www.sanfoundry.com/c-program-shutdown-computer/ and since I don't know what the -P flag does, I'd advise you to research before trying it! (I tested it on my Pi though)

klricks
Posts: 7159
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: How can the raspberry be shutdown with a python script and no sudo?

Tue Apr 24, 2018 12:16 pm

pcmanbob wrote:
Tue Apr 24, 2018 10:07 am
klricks wrote:
Shutdown without sudo:

Code: Select all

#!/usr/bin/python
import subprocess
subprocess.Popen(['shutdown','-h','now'])
just tried this on stretch light using the pi user and it does not work
you get this error message

Code: Select all

Failed to set wall message, ignoring: The name org.freedesktop.PolicyKit1 was not provided by any .service files
Failed to power off system via logind: The name org.freedesktop.PolicyKit1 was not provided by any .service files
Failed to open /dev/initctl: Permission denied
Failed to talk to init daemon.

just as I suspected you would because you always need sudo to run shutdown.
The ability to shutdown without sudo was added to Raspbian DESKTOP quite some time ago. I don't know about the lite version?
The program I posted above works for me as well as running shutdown -h now from the command line.
Unless specified otherwise my response is based on the latest and fully updated RPiOS Buster w/ Desktop OS.

bbqbailey
Posts: 10
Joined: Fri May 18, 2018 5:18 pm

Re: How can the raspberry be shutdown with a python script and no sudo?

Mon Dec 17, 2018 4:19 pm

Douglas Lawson:

I tried your suggestion but it doesn't work for me:

Your suggested code:
pi raspberrypi =NOPASSWD: /usr/bin/systemctl poweroff,/usr/bin/systemctl halt,/usr/bin/systemctl reboot

Making the following changes (my systemctl is in /bin/systemctl), and my system name is RPI-Dev
pi RPI-Dev =NOPASSWD: /bin/systemctl poweroff,/bin/systemctl halt,/bin/systemctl reboot

Here is the error I get after rebooting the system via "sudo reboot" to make sure the changes to /etc/sudoer are picked up:

pi@RPI-Dev:~ $ reboot
Failed to set wall message, ignoring: Interactive authentication required.
Failed to reboot system via logind: Interactive authentication required.
Failed to open /dev/initctl: Permission denied
Failed to talk to init daemon.

My system info: cat /etc/os-release

PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

Do you have any other suggestions?

Thanks!

User avatar
DougieLawson
Posts: 39182
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How can the raspberry be shutdown with a python script and no sudo?

Mon Dec 17, 2018 4:44 pm

/sbin/reboot needs an entirely seperate line in your sudoers file.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

bbqbailey
Posts: 10
Joined: Fri May 18, 2018 5:18 pm

Re: How can the raspberry be shutdown with a python script and no sudo?

Mon Dec 17, 2018 4:53 pm

DougLawson: Thanks, but now I'm confused. Your code entry shows reboot on the same line; I just copied and pasted your code, while changing the location for the commands to /bin/ instead of /usr/bin/

So, are you saying your line, that contains "reboot" isn't correct?
pi raspberrypi =NOPASSWD: /usr/bin/systemctl poweroff,/usr/bin/systemctl halt,/usr/bin/systemctl reboot

Thanks, appreciate your help!

User avatar
DougieLawson
Posts: 39182
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How can the raspberry be shutdown with a python script and no sudo?

Mon Dec 17, 2018 4:58 pm

Reboot is a parameter for the systemctl program

Code: Select all

pi raspberrypi =NOPASSWD: /sbin/reboot
should work, but I can't be bothered to test it.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

bbqbailey
Posts: 10
Joined: Fri May 18, 2018 5:18 pm

Re: How can the raspberry be shutdown with a python script and no sudo?

Mon Dec 17, 2018 5:38 pm

OK DougieLawson, found the problem; operator error.

Your code works.

What I didn't understand, was that I would still have to type sudo, as in "sudo reboot"; doing so causes the reboot without having to supply the password for the sudo command. I thought I would just have to type "reboot".

Thanks again

fbe
Posts: 642
Joined: Thu Aug 17, 2017 9:08 pm

Re: How can the raspberry be shutdown with a python script and no sudo?

Mon Dec 17, 2018 6:48 pm

jbudd wrote:
Tue Apr 24, 2018 12:00 pm
You can do it with a root owned setuid executable.

But it has to be a compiled program - setuid is ignored by shell scripts (and Python, I guess)

Code: Select all

/*sd.c  C Program to Shutdown or Turn Off the Computer in Linux.*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
  system("shutdown -P now");
  return 0;
}
...

Code: Select all

mkdir -p ~/bin
cat > ~/bin/shutdown << _EOF_
#!/bin/sh
echo 'ALWAYS USE ABSOLUTE PATH IN system("...") FUNCTION IF YOUR PROGRAM IS EXPECTED TO RUN WITH ROOT PRIVILEGES'
_EOF_
chmod +x ~/bin/shutdown
Logout, login and run your sd program.

Return to “Beginners”