-1

What is the best practice for restarting an ubuntu system service from within a python program? The service is responsible for starting the same program where the restart would be called from. The service script has stop,start and restart methods. The stop method is supposed to kill the running processes originally executed in the start method. I have been using subprocess.call method in python however the program has a Flask session and when I call restart on the program the scripts began execution but I get an "Address already in use error" indicating that the flask session has not been ended.

Similar question: Python spawn off a child subprocess, detach, and exit

rigger12
  • 90
  • 9

1 Answers1

3

The best way would be to send a signal (e.g. SIGTERM) to all processes started in the start method. The "Address already in use error" could be caused by the Flask program not shutting down properly.

  • Thanks but I am already using "pkill" within the stop() method to kill the original processes. I don't have any issues executing a service restart from terminal. It is only when I call a service restart from the python of the running flask program. I would think since subprocess.call spawns a new process it would be able kill the process it was called from, but maybe there is a conflict. – rigger12 Jun 14 '17 at 14:07
  • 1
    This situation I normally handle by having the spawned process detach itself from its parent. The simplest approach for me is to run the program wrapped in a shell script and have the shell disown it. – Borko Jandras Jun 14 '17 at 15:18
  • Thank you for the suggestion. I am looking into doing this with os.fork() in python. – rigger12 Jun 14 '17 at 16:29