Restart services or delete files that need root privileges using Python


We cannot remove files like PID that need root privileges in Python. We also can’t restart services on “/etc/init.d” using Python.
At least, we can’t do it directly using “os.unlink” or “subprocess”. Because, we will caught by this errors that said:

1
Python remove file permission denied

How to solve this problem?
This is easy, we can use Fabric with sudo(“command”) to overcome this problem.

1. Example restart services using Fabric

1
2
3
4
def restart_services():
    with cd(‘%s’ % PROJECT_PATH):
        sudo(‘/etc/init.d/redis_6379 start’)
        sudo(‘/etc/init.d/nodejs restart’)

We can use “sudo” with fabric without any hassle.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.