I’m trying to kill a process (specifically iChat). On the command line, I use these commands:
ps -A | grep iChat
Then:
kill -9 PID
However, I’m not exactly sure how to translate these commands over to Python.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Assuming you’re on a Unix-like platform (so that
ps -Aexists),gives you
ps -A‘s output in theoutvariable (a string). You can break it down into lines and loop on them…:(you could avoid importing
signal, and use9instead ofsignal.SIGKILL, but I just don’t particularly like that style, so I’d rather used the named constant this way).Of course you could do much more sophisticated processing on these lines, but this mimics what you’re doing in shell.
If what you’re after is avoiding
ps, that’s hard to do across different Unix-like systems (psis their common API to get a process list, in a sense). But if you have a specific Unix-like system in mind, only (not requiring any cross-platform portability), it may be possible; in particular, on Linux, the/procpseudo-filesystem is very helpful. But you’ll need to clarify your exact requirements before we can help on this latter part.