I need my Django app to be able to restart the Apache server running it. I'm using mod_python.
Which is the easiest way?
The only way this would be possible is if you gave your Django application root permissions, which is horribly unsafe. But, assuming that your app is running as root, you can run
import subprocess
subprocess.Popen(['/etc/init.d/apache2', 'restart'])
Or whatever command your distribution has for restarting Apache. If the Django app is not root, you may be able to run it with sudo
using the pexpect python library.
import pexpect
child = pexpect.spawn('sudo /etc/init.d/apache2 restart')
child.expect('[sudo] password for .*:')
child.sendline(password)
child.interact()
Note that this way requires you to give the apache user the ability to run sudo
which is also insecure. In general, I can't see a secure way to accomplish this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.