2017-08-31 41 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添加到無密碼列表中。現在它像一個魅力。