On Sunday 06 April 2008 12:44:54 Colm Buckley wrote:
> On Sun, Apr 6, 2008 at 12:29 PM, Ian <ianhoweire at gmail.com> wrote:
<snip>
> then i want to have another script that can kill the process.
>> > I presume i am only able to kill the process using the pid using the
> > format
> > 'kill 1234'
> > I can use the following line to copy the pid to a file
> >
> > ps -e | grep firefox-bin | cut -d ' ' -f2 > ~/processID
> >
> > how then can i pass the entry in processID to the kill command.
> > Or if there is another way to do this i''m all ears.
>> killall firefox-bin
>> Or, if you really do want to do it using the above:
>> kill `ps -e | grep firefox-bin | cut -d ' ' -f2`
>> Note the backticks. Most modern shells will also allow $(), so:
>> kill $(ps -e | grep firefox-bin | cut -d ' ' -f2)
this will only work for PIDs between 1000 and 9999 - the output of ps -e is
formatted with the integer value right-justified (presumably with a "%5d" in
a printf statement):
1
10
100
1000
10000
cut simply splits a string according to the delimiter you give, and returns
the parts you request, so
$(ps -e | grep firefox-bin | cut -d' ' -f2)
will return "" for numbers less than 1000, and either the terminal -
e.g. "pts/1" - or "?" for numbers greater than 9999.
You should use awk instead in this situation:
kill $(ps -e | grep firefox-bin | awk '{print $1}')
>> will also work. killall is easier, though. Note : I believe the Linux
> killall has different semantics from the Unix one; if you run killall on
> (eg:) a Solaris system, it will send a signal to all processes, not just
> the named ones.
"pkill" is a likelier candidate here - it works much the same way on Solaris
and Linux. It can also accept a user id in case you don't want to
get "Operation not permitted" error messages if another user is running
firefox on the same system - or worse, kill the processes of other users if
you happen to be running your script as root.
pkill -u $USER firefox-bin
Note that sending kill signals to firefox in this way will result in an error
message about your last session closing unexpectedly next time you start it -
rather silly behaviour from a web browser, I reckon. Does anyone know how to
turn this behaviour off?
/Ciaran.
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!