Cut command usage in (147 Views)
Reply
Advisor
sammer
Posts: 24
Registered: ‎10-12-2008
Message 1 of 5 (147 Views)
Accepted Solution

Cut command usage in

Hi,

$ ps -aef |grep mountd*
root 546 1 0.0 Oct 09 ?? 0:00.03 /usr/sbin/mountd -n -i -n
root 562 1 0.0 Oct 09 ?? 0:00.00 /usr/sbin/automount -m /net -hosts -nosuid
root 45657 13758 0.0 10:18:27 pts/0 0:00.01 grep mountd*

My main objective is to check for existing daemon (mountd) runs.If it runs i must kill particular pid's.

Say here i must do "kill -9 546" and kill -9 562". Please let me know how to use CUT or AWK command to kill above two pid's..

Thanks in Advance,
Shammer

Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,381
Registered: ‎03-06-2006
Message 2 of 5 (147 Views)

Re: Cut command usage in

kill -9 $(ps -ef | grep -e "[m]ountd" -e "[a]utomount" | awk '{print $2}')

This works if you have a real shell, ksh. Add "echo" to the beginning to make sure it's correct.

Note: If you were on HP-UX you would never use grep but the -C option:
kill -9 $(UNIX95=ENHANCED_PS ps -opid= -C mountd,automount)
Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,036
Registered: ‎02-23-2005
Message 3 of 5 (147 Views)

Re: Cut command usage in

First, what go you think that
grep mountd*
is doing? Hint:
mkdir new_dir
cd new_dir
and try your command again.

Second, you can eliminate your "grep" command
from the "ps" listing by using a popular
trick:

urtx# ps -aef | grep [m]ount
root 478 1 0.0 00:31:37 ?? 0:00.00 /usr/sbin/mountd -i
root 493 1 0.0 00:31:38 ?? 0:00.00 /usr/sbin/automount -m /net -hosts -nosuid

Third, I can remember how to use "sed", but
I'd need to read up on "awk" or "cut".

urtx# ps -aef | grep [m]ount | sed -e 's/ *[^ ]* *\([^ ]*\) .*/\1/'
478
493

If it's not obvious how to do the "kill"
commands from a list of pid's, then you might
try something like this:

urtx# ps -aef | grep [m]ount | sed -e 's/ *[^ ]* *\([^ ]*\) .*/\1/' | \
while read pid ; do echo kill ${pid} ; done
kill 478
kill 493

When you're happy, remove "echo".

Now that we've solved _that_ problem, what is
the _real_ problem which you are trying to
solve? And why would killing these processes
be the best way to solve it?
Please use plain text.
Honored Contributor
Steven Schweda
Posts: 9,036
Registered: ‎02-23-2005
Message 4 of 5 (147 Views)

Re: Cut command usage in

And you had better hope that no one else runs
anything which your "grep" will falsely
interpret as one of the processes you're
trying to kill. Or else do a much more
precise search for the processes which you
_really_ wish to kill (user, parent pid, more
complete path, and so on), which would be
harder to do on one line.

> Note: If you were on HP-UX [...]

Yeah. I'd convert all _my_ software to get a
"ps" with that feature.
Please use plain text.
Advisor
sammer
Posts: 24
Registered: ‎10-12-2008
Message 5 of 5 (147 Views)

Re: Cut command usage in

Hi,

Thanks a lot for time on this..

Closing this thread..

Thanks,
Shammer
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation