kill a process by name

Hello.
This has been a question the last couple of days, and it seems to be a recurring topic in the forums
therefore I post this little handler that should kill most processes for which you as a user with your rights are granted access to kill.
It works best with foreground processes that are not showing up in the dock, where the KillAll command wont work that is the intent. It should work equally well with shell processes as long as you that the (process name is unique) or else you are killing processes randomly.

Some people will say that this is an uttermost brutal handler, because it will kill the process with no nonsense like letting the process saving its environment and such.

It works however all of the time, not only some of the time.
Usually I’m capable of assembling these command lines by my self, but this one was given to me
by Yvan Koenig (But I upgraded the severity of the “kill” from −2 to −9, and made it into a handler.)
It won’t work on a process which has a hyphen in its name. Like Tex-Edit Plus.


” killProcess("TextEdit")
on killProcess(processName)
	set cmd to "kill −9"
	set pipe to "`ps ax | grep \"" & processName & "\" | grep -v grep | awk '{ print $1 }'  `"
	do shell script cmd & space & pipe
end killProcess

Best Regards

McUsr

Can I ask you, and I know it has nothing to do with killing a process, if there’s any good documentation about the “grep” command? I have seen it almost everywhere and it seems like a very nice and helpful tool.

Thanks in advance,
ief2

Thank you! Very usefull…

Hello Ief.

Well yes, you have the man command. And you should really see if google can offer some more info.

If your desires to learn passes just the mere comprehension of the switches and so on, then I have some book titles for you. :slight_smile:

There is some books which hopefully is on the list I gave you that you should read, but any shell scripting primer would do. Unix is more about text processing - and programming than file managment.

Brian W Kernighan and Robert B. Pike: The Unix Programming Environment is probably the best introduction to Unix Ever. They really shows you how you use the commands to get jobs done.
Another great book is Stephen R. Bourne: The Unix System V Environment.
If you can’t get hold of any of those two then you could look for a book by Mark Sobell which I believe is titled Unix System V (something) But the two first are written by programmers for programmers.

The bash manual man bash is also a mandatory read.

Grep is just one command in a family of commands, you should also have a look int sed, which it derived from, ed is another at times very useful editor to use in “here” documents which you should know something about. Awk is another program which you can use to build your own relational dbms with though primitive. One of the All time Great Books treats this language in “The Awk Programming Language” By Aho, Weinberger and Kernighan. -If you can get hold of it. :slight_smile: Those programmers mentioned here, is really among the greatest of all times, and so are their writing abilities, and abilities to explain the material.

There are of course good online tutorials as well. Don’t dwell to long into this, when you have gotten the basics
and are able to program

Best Regards

McUsr

Thank you McUsr. I will definitely have a look at those books and see if I can find them anywhere.

I really appreciate the input,
ief2

google grep tutorial, there are some useful sites

In fact, I got the script from Thomas Fischer in response to a question asked on applescript-users@lists.apple.com.

Maybe using
quote form of processName
get rid of the hyphen problem.

I can’t test because I don’t own Tex-Edit Plus.

Yvan KOENIG (VALLAURIS, France) jeudi 17 juin 2010 18:04:09

Hello Yvan.

I can’t get around the hyphen problem. Unsolvable just try (and have it working with other processes afterwards,) If anybody manages, I’ll be happy to amend the changes.

I could remove the “escapisms” around grep though. -And will do.

Best Regards

McUsr