OT - shell script to kill a process

Ken Ray kray at sonsothunder.com
Mon Jul 9 18:03:35 EDT 2007


On Mon, 9 Jul 2007 14:46:32 -0600, Chris Sheffield wrote:

> My Unix scripting knowledge leaves a bit to be desired, so I thought 
> I'd ask here for some help.
> 
> I have a Vise installer for OS X that needs to check for and kill our 
> own process if it's running. The installer will be authenticated when 
> running. I need a shell script that I can execute from within the 
> installer that will determine, by name, if a given process is 
> running, and then kill it dead if so. Can someone help?

Well, it's ugly, but you can execute this:

ps -awx | grep 'TextEdit' | grep -v 'grep' | cut -d\  -f2 | xargs -I 
pid kill -9 pid

A few notes:
  - I'm using TextEdit as the app I want to close - replace your app 
name here (to see what I'm parsing, execute 'ps -awx' in the Terminal 
by itself).
  - This is all one line, no returns here
  - There are actually two spaces after the "d\" and before the "-f2".

Here's what it means (for those wondering):

  (ps -awx) = Get a list of all currently running processes with full 
path names.

  ( | grep 'TextEdit') = Pipe the result to 'grep' (the regex engine) 
and return any lines that contain 'TextEdit'. This will return TWO 
lines, one with the path to TextEdit on it, and the other one is the 
actual 'grep' call that is trying to find 'TextEdit'.

  ( | grep -v 'grep') = Pipe the result to 'grep' again, but this time 
ignore any lines that have 'grep' in it. (Sneaky!)

  ( cut -d\  -f2) = Extract ("cut") the second space-delimited "word" 
in the resulting string ("-d" means use a delimiter, "\ " is the 
delimiter to use (has to be escaped because spaces normally signify a 
change of parameters, etc. on the command line), "-f2" means look for 
the second space-delimited "field" in the string)

  ( | xargs -I pid kill -9 pid) = Pipe the result (the process ID) into 
a variable called 'pid' that will replace the argument variable 'pid' 
in the call to the 'kill' command (normally to kill a process it would 
look like "kill -9 1012")).

HTH,

Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/



More information about the use-livecode mailing list