Terminate a python script from linux shell / bash script

Nigel Rantor wiggly at wiggly.org
Thu Jul 31 13:21:55 CEST 2008


Gros Bedo wrote:
> Thank you guys for your help. My problem is that I project to use this command to terminate a script when uninstalling the software, so I can't store the PID. This command will be integrated in the spec file of the RPM package. Here's the script I'll use, it may help someone else:
> 
> #!/bin/sh
> # PYTHON SCRIPT PROCESS KILLER by GBO v0.1
> # This script will look for all the lines containing $SOFTWARENAME in the process list, and close them
> 
> SOFTWARENAME='yoursoftware' #This is case insensitive
> JOBPRESENT=$(ps -ef | grep -i $SOFTWARENAME | grep -v grep)
> echo $JOBPRESENT
> ps -ef | grep -i $SOFTWARENAME | grep -v grep | awk '{print $2}' | xargs kill

If you have a long running process you wish to be able to kill at a 
later date the normal way of doing it would be for the script itself to 
write it's own PID to a file that you can then inspect from a different 
process and use to kill it.

So, my_daemon.py might shove its PID into /var/run/my_daemon.pid

And later my_daemon_killer.py (or indeed my_daemon_killer.sh) would read 
the PID out of /var/run/my_daemon.pid and pass that to a kill command.

Using ps/grep in the way you're trying to do is always going to be 
inexact and people will not thank you for killing processes they wanted 
running.

   n



More information about the Python-list mailing list