1

I need to create startup and stop scripts for a python program. My temporary solution was to have the startup.sh:

python -m root.scripts.run 
python server.py 'localhost' 42345

This starts my program and a server that is needed to do some computations. Now for stop.sh I just do:

killall -m Python
killall python
killall Python

This works and stops my program. However I need to find a less "brutal" solution, since this obviously kills all python related processes. A solution I'm thinking of (not sure if it is possible) is in the startup.sh to get the PID of the two started processes and store them in a file somewhere. Then for the stop.sh I would just get those pids and kill those processes. Now I have a few questions:

  1. Is my proposed solution viable? If so how can I get the pid of the recently started processes ? In terms of storing them and then getting them from a file, is this doable and if so how?
  2. Do you have any other proposed solutions for my problem?
  3. In terms of cross-platform, how hard would it be to mimic something like this on windows?

2 Answers 2

4

Use the subprocess module to start the program from Python (this is the recommended way anyway). The object returned from subprocess.Popen has terminate() and kill() methods as well as a pid attribute that you can save and use for stopping the process again.

On Unix systems, you might consider saving the PID in a file in /var/run/<yourscript>.pid (see the Filesystem Hierarchy Standard), then read this file to retrieve the PID when requested to stop.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the input. But I'm not sure if I understand your suggestion. So you are saying I should replace my startup.sh shell script with a python script. Then in this python script use subprocess.Popen(root.scripts.run) subprocess.Popen(server.py). From this i get my PID's and store them somewhere. Then replace the stop.sh shell with a python script also and get those pids and kill the processes. Is this correct ?
Maybe I misunderstood your question. Why don't you just write an init shell script based on some existing one? For example, Debian systems come with a skeleton file /etc/init.d/skeleton that you can easily adapt to start your Python program.
1

I would replace your startup.sh for a Python script which runs both scripts. Use subprocess.Popen() for running new processes and terminate() for killing them.

Python solves your third question. Otherwise you have to use cygwin/mingw under Windows.

2 Comments

But I need to be able to call start and stop separately. I mean the user should be able to call let's say python start_program.py to start the application. Then if something goes wrong or he just wants to stop he would python stop_program.py. Your suggestion would be to create only one Python script which would be started like python app.py "start" respectively python app.py "stop" ?
Exactly. That's the idea: Use app.py [start|stop] and parse the argument from the command line. You could even add more flags or arguments by using argparse module. This approach works both Linux and Windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.