2017-08-31 43 views
0

我试图从浏览器重新启动mongodb。这里是我用django框架编写的代码:Mongodb不会重新启动子进程调用

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

通过检查apache2的日志,我发现我需要使用www-data ALL=(ALL) NOPASSWD:ALLwww-data添加到无密码列表中。现在它像一个魅力。