我试图从浏览器重启mongodb。这是我用django框架编写的代码:
def restart_db_mongo(request):
if request.method == 'POST':
restart_cmd = ["sudo","systemctl","restart","mongodb"]
p = subprocess.Popen(restart_cmd,stdout=subprocess.PIPE)
(output, err) = p.communicate()
if err is not None:
return JsonResponse({"err_code":1,"err_description":str(err)},safe=False)
status_cmd = ["sudo","systemctl","status","mongodb"]
p = subprocess.Popen(status_cmd,stdout=subprocess.PIPE)
(output, err) = p.communicate()
if 'active (running)' in output:
return JsonResponse({"err_code":0,"err_description":"Restarted successfully!"},safe=False)
else:
return JsonResponse({"err_code":1,"err_description":str(err)},safe=False)
else:
return JsonResponse({"err_code":2,"err_description":"Bad request"}, safe=False)
我的问题是mongodb没有重新启动。实际上,它完成了第一个子进程调用而没有错误,但状态仍然保持不活动状态。在交互式环境中,我已经验证了命令是否有效。我使用apache2来运行我的Web应用程序。
答案 0 :(得分:0)
通过检查apache2的日志,我发现需要使用www-data
将www-data ALL=(ALL) NOPASSWD:ALL
添加到无密码列表中。现在它就像一个魅力。