简体   繁体   中英

How to start/stop service from python script running in Flask and Apache server using Debian?

I'm trying to start and stop services from a python script that is running using Flask and Apache.

To get the status from memcached, for example, I'm using

os.popen('service memcached status').read() and works like a charm.

The problem is that when I try to start/stop doing something like

os.popen('service memcached stop').read() it just does nothing (I checked by the shell that the service is still running)

To summarize, I can get the status but can't start/stop and don't know why its happens.

Does anyone have any suggestion?

Thanks,

I saw the apache logs in /var/log/apache2/error.log and the problem was that I needed more privileges to execute start/stop. But when I tried to use

os.popen('sudo service memcached stop').read()

I got an error, saying that I should have typed the su password.

To solve this problem I typed in the shell:

visudo

which opened the /etc/sudoers file. And there I added the line

www-data ALL=(ALL) NOPASSWD:ALL

I understood that this means that I am giving permission to the user www-data execute sudo without password.

To quit, press Ctrl+X and then y to save.

note: www-data is the username that executes the apache.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM