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.