Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A bash script to kill a process UNIX
I was talking to my Unix friend about killing apps easily, from the Terminal. This is what we came up with. Just slide it into your .bashrc, and you'll be able to killapp brokenapp!
 function psapp() {
ps -ax | grep -i $1 | grep -i -v "grep.-i.$1" | awk '{print $1}'
}
function killapp() {
kill $(psapp $1)
}
The first function lists the process id for the argument that you request. So psapp word will list the process id for word.

Breaking it down, ps -ax lists the processes, piping to grep -i lists just the lines that have the argument you entered. Unfortunately, it also lists grep and the argument, since that's a running process as well. Therefore, the second grep excludes the grep process. Finally, awk parses for the first column. The second function, killapp, takes the process id as an argument and kills the running process.

To kill an app, just type 'killapp appname' ... or partial app name. It's case insensitive to boot!

[Editor's note: I don't have the bash shell, so I haven't tested this one.]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[13,345 views]  

A bash script to kill a process | 11 comments | Create New Account
Click here to return to the 'A bash script to kill a process' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use killall
Authored by: kerbaugh on Jan 11, '03 04:09:06PM

Why not just use the killall command? It has a switch for regular expression match and more important, it has switches to make it interactive and to limit the scope of processes it will kill. I would think this much safer as people unfamiliar with regular expressions might kill a lot more than they bargained for.



[ Reply to This | # ]
Use killall on iTunes
Authored by: strandcos on Jan 11, '03 05:15:15PM

I like to listen to music when I go to bed. I could not find a "sleep" command in iTunes, so I use:
(sleep 3600;killall iTunes)&
From a shell, and it turns off iTunes after an hour. The number, obviously, is seconds.



[ Reply to This | # ]
Sleeping iTunes
Authored by: punkmanandy on Jan 11, '03 08:05:34PM

you can get a simple gui application called "iSleep" which will do the same thing, and more, but with a gui,



[ Reply to This | # ]
Use killall
Authored by: Erik Toh on Jan 11, '03 11:29:10PM

I'm not on OS X now but IIRC Apple does not include `killall` in any of the OS releases.



[ Reply to This | # ]
killall is available
Authored by: mingking on Jan 12, '03 12:52:35AM

It's on my machine. 10.2.3



[ Reply to This | # ]
Use killall
Authored by: thatch on Jan 12, '03 05:25:57PM

Have a look in /usr/bin because it's in there on my rig, 10.2.3.

And bash is in /bin too.



[ Reply to This | # ]
no bash???
Authored by: gralem on Jan 11, '03 07:02:26PM

To The Editor: You may not have bash, IF YOU\'RE NOT RUNNING OSX 10.1 or 10.2. I\'m not 100% sure that this is the case. You may want to try \"/bin/bash\".

---gralem



[ Reply to This | # ]
bash shell problems
Authored by: rsnyder on Jan 11, '03 08:05:34PM

I am running 10.2.3. In the past (not sure if since up grading to Jag) I have had the bash shell as my default, but I changed it a while ago. Now when I try to change back to the bash I am getting the following:

dyld: -bash Undefined symbols:
_tgetent
_tgetflag
_tgetnum
_tgetstr
_tgoto
_tputs
[Process was terminated by signal 5]

I removed all the bash profile config files in my ~ directory and in /etc in case they were corrupt, but same error. tcsh works fine.

Any suggestions?



[ Reply to This | # ]
bash shell problems
Authored by: Erik Toh on Jan 11, '03 11:26:53PM

You need to upgrade your version of bash.



[ Reply to This | # ]
bash shell problems
Authored by: rsnyder on Jan 12, '03 10:44:29PM

I have reinstalled 2.05 from the link on VersionTracker and from the Apple site. No change. Other thoughts?



[ Reply to This | # ]
bash shell problems
Authored by: MattyG on Jul 28, '05 02:06:31PM

Under Terminal->Preferences try to execute /bin/tcsh or if that doesn't work try /usr/bin/tcsh

worked for me.



[ Reply to This | # ]