How to start/kill application with python script?

12,716

Solution 1

The implementation-independent concept (things you need to know whether you chose Python or bash or something else):

In order to reliably 'kill' another process you need to know its process ID and then send it a signal (such as SIGTER). It depends on the other process how it handles SIGTER (it can ignore it, but it can also invoke shutdown code and properly terminate). The signal SIGKILL kills the other process the hard way, it does not have any chance to cleanly shut down.

In order to start another process from a given executable, you need to know the path to this executable or this executable must be a valid command (as can be controlled with the PATH environment variable).

Your if pyApp is running line requires knowledge about the process ID (PID) of the application you want to monitor and finally kill. Identification of the PID just by name can work, but is not reliable (there could be more than one process with the same name). In order to have a reliable correspondence between process ID and your application of interest, the monitoring 'script' should be the one that has started the application in the first place. It should be the program that entirely manages the application. A good reference is http://mywiki.wooledge.org/ProcessManagement.

bash details

In bash, you can get the process ID of the process that you started last using $!. Most systems also have the pgrep or pidof commands available by which you can find the IDs of processes with a certain name.

A simple idiom would be:

app & # Start a new process with your app in the background.
APP_PID=$! # Remember its PID.
kill $APP_PID # Send SIGTER to process.

Python details

In Python, I'd recommend to use the subprocess module. You can spawn any executable via p = subprocess.Popen(...). You can then control the subprocess in a programmatic fashion (send data to stdin, terminate the process, retrieve its PID for later usage, get its exit code, ...).

Again, a simple and yet reliably working example would be:

import subprocess

p = subprocess.Popen(["tail", "-f", "~/.bashrc"])
print "Process ID of subprocess %s" % p.pid
# Send SIGTER (on Linux)
p.terminate()
# Wait for process to terminate
returncode = p.wait()
print "Returncode of subprocess: %s" % returncode

If you need to keep things simple and are working with Linux anyway, I'd start implementing a basic solution in bash first. You'll probably end up with less code. In both cases, you can make severe mistakes. In the end, it's a matter of taste.

Solution 2

This is perfectly doable in Python using os.kill and a variant of os.spawn.

Share:
12,716

Related videos on Youtube

Alex
Author by

Alex

Passionate about innovations. Invented some stuff. In the first 50 years of my life, didn't have to learn coding. But seems, now got to. In love with electronic engineering, music, chess, Linux, Python...aaaannnnd inventions! I like to help people, but not much - don't like to discomfort my selfishness.

Updated on September 18, 2022

Comments

  • Alex
    Alex over 1 year

    I use Linux (LMDE) and I'm wondering how to make simple .py script to start and stop another python app. For instance :

    if pyApp is running :
       kill pyApp
       exit xterminal
    
    else :
       start xterminal
       python pyApp.py
    

    Is it appropriate with python or should I use bash?

  • Dr. Jan-Philip Gehrcke
    Dr. Jan-Philip Gehrcke almost 11 years
    Instead of using these basic OS commands from within Python, one could directly use the corresponding commands in a shell script. If one wants to use Python, one should use its subprocess module for these tasks.
  • Alex
    Alex almost 11 years
    Wow...I got more than I asked. I appreciate your time to clarify this matter in such broad way. Does bash could swallow conditions like if, else...?
  • Dr. Jan-Philip Gehrcke
    Dr. Jan-Philip Gehrcke almost 11 years
    Do you mean if bash supports conditional statements? Sure, looks like you have to work through a very basic bash tutorial... :-).