Terminate a python script from linux shell / bash script

Piet van Oostrum piet at cs.uu.nl
Thu Jul 10 16:50:06 EDT 2008


>>>>> Gros Bedo <gros_bedo at hotmail.com> (GB) wrote:

>GB> Yes I've seen that each python script calls its own instance of
>GB> Python. But how to know which is the good one in bash ? Is there a
>GB> command that gets the parameters of process, so I could use grep to
>GB> select the one containing the name of my script ?

The ps command will usually give you a list of the running processes with
their argument, but using that is suboptimal.

I suppose you start the process in the background, like: python myscript &.
When you start the python script in bash (or any other process for that
matter) in the background you can get the process id (pid) with $!
(immediately after starting the process). Later on you can use this to kill
the process:

python myscript myargs &
savepid=$!


later: kill $savepid

That is much better than trying to grep through the ps output.
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list