Apache hosted Python Flask web app running in Rocky Linux 9 not able execute shutdown command

49 views Asked by At

I'm new to Python Flask application web hosting, in the web app I'm trying to set functionality to shutdown server once every work is complete. I have built the application in Flask Jinja Python virtual environment and hosted by wsgi using Apache web service. User apache is having root privileges to execute the script, and complete permissions.

The whole Rocky Linux OS is running in Raspberry Pi4

Problem

When I try the below options to shutdown which is hosted by Apache, I'm getting below error in Apache error_log file.

sudo: PAM account management error: Permission denied
sudo: unable to open audit system: Permission denied
sudo: a password is required

python init.py file

data = 'yes'
down(data)

def down(cname = None):
    try:
        if cname == 'yes':
            print (cname)
            if os.name != 'nt':
                #os.system("sudo shutdown -h now")
                #os.system('sudo systemctl poweroff')
                #subprocess.check_output("sudo /sbin/shutdown -h now", shell=True, universal_newlines=True)
                subprocess.call(["sudo", "shutdown", "-h", "now"])
                #os.system('sudo bash [complete path to the script]/off.ksh')
                #os.system('python [complete path to the script]/test.py')
                #subprocess.run(["python", "[complete path to the script]/test.py"])

            else :
               os.system('shutdown /s')
            return

except Exception as e:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        e = str(e) + str(exc_type) + str(exc_tb.tb_lineno)
        return Exception(e)

off.ksh

#!/bin/bash
sudo shutdown -h now

test.py

import os

if os.name != 'nt':
    print(os.name)
    os.system('sudo shutdown -h now')
else :
    os.system('shutdown /s')`

How can I resolve this?

1

There are 1 answers

5
Ricardo Gellman On

You could achieve this by doing something nasty in permissions, but so far, possible.

Set in sudoes the user that will execute this script, as show below

your_username ALL=(ALL) NOPASSWD: /sbin/shutdown