Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start/kill application with python script?

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?

like image 919
Alex Avatar asked Dec 15 '22 08:12

Alex


2 Answers

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.

like image 173
Dr. Jan-Philip Gehrcke Avatar answered Dec 31 '22 16:12

Dr. Jan-Philip Gehrcke


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

like image 41
hivert Avatar answered Dec 31 '22 16:12

hivert