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?
2
Answers
I have tried to Linux Ubuntu version 22.04 the setup works well now as expected with above set of commands, as Rocky Linux 9 has some limitations, and it is not stable version tho.
I have used command in python code.
os.system("sudo shutdown -h now")
and given necessary permission to user to shutdown.
Thanks for all the good hearts those helped.
You could achieve this by doing something nasty in permissions, but so far, possible.
Set in
sudoes
theuser
that will execute this script, as show below