title: Python 3 - Flask Restful API to restart another API
body: <p>I created a python REST web-service, and it will execute a series of subprocess commands to restart another python REST webservice. But the problem is it doesn't really start my other REST service after I call it.</p>
  
  <p>Here is my code for the REST API :</p>
  
  <pre><code>from flask import Flask, jsonify, request, make_response
  from flask_restful import fields, reqparse, abort, Api, Resource
  import sqlalchemy
  from sqlalchemy.sql import table, column, select, update, insert, delete, func
  from sqlalchemy.orm import sessionmaker
  import requests
  import time
  import traceback
  import subprocess
  import os
  
  app = Flask(__name__)
  api = Api(app)
  
  class RestartPython(Resource):
      def get(self):
  
          command1 = 'fuser -k 9876/tcp'
          proc1 = subprocess.Popen(command1, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
          proc1.wait()
  
          print('kill python process done')
  
          command2 = 'source activate envpy3'
          proc2 = subprocess.Popen(command2, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
          proc2.wait()
  
          print('switch python env done')
  
          command3 = 'python ReceiveCall.py'
          print(command3)
          proc3 = subprocess.Popen(command3, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
          proc3.wait()
  
          print('ReceiveCall started')
  
          return make_response(jsonify({'message': 'Python webservice is restarted', 'status': 'Ok'}), 200)
  
      api.add_resource(RestartPython, '/restart')
  
  if __name__ == '__main__':
      app.run(host='0.0.0.0', port=8082, debug=True)
  </code></pre>
  
  <p>This will works if I just run it without wrapping them in web-service</p>
  
  <pre><code>import subprocess
  
  if __name__ == '__main__':
      command1 = 'fuser -k 9876/tcp'
      proc1 = subprocess.Popen(command1, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
      proc1.wait()
  
      print('kill python process done')
  
      command2 = 'source activate envpy3'
      proc2 = subprocess.Popen(command2, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
      proc2.wait()
  
      print('switch python env done')
  
      command3 = 'python ReceiveCall.py'
      print(command3)
      proc3 = subprocess.Popen(command3, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT, shell=True)
      proc3.wait()
  
      print('ReceiveCall started')
  </code></pre>
  
  <p>Any ideas?</p>