Skip to contentSkip to conversation

Extraction of PID

DISCUSSION Peter 4 participants 5 messages Jul 26, 2003 – Jul 27, 2003

lore
Share & tools
lore

Peter

author
#0 lore raw

Hi,

How do I isolate from a line like

1865 tty2 00:00:18 exmh

the first 4 numbers which is the PID. I get it with "ps -a --pid 1111 | grep exmh".

Thanks & regards

Signature · 7 lines
Peter

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#1 lore raw
Quoted 8 lines · Peter

Hi,

How do I isolate from a line like

1865 tty2 00:00:18 exmh

the first 4 numbers which is the PID. I get it with "ps -a --pid 1111 | grep exmh".

Hi,
As usual in 'nix there are several different ways of doing this. My first guess was:

ps -a -pid 1111 | grep exmh | awk '{print $1}'

Which works fine for me on my Redhat box.

Second guess was:

ps -a -pid 1111 | awk '/exmh/{print $1}'

Which also worked fine.

Since not many people use awk, I though I would try a few examples with the cut command.

ps -a -pid 1111 | grep exmh | cut -d' ' -f1

This example works but I had to use the -d option to specify the delimiter. If you were sure the pid was only going to be four characters long, you could use:

ps -a -pid 1111 | grep exmh | cut -c1-4

I have just spent the past ten minutes messing about using sed to do this too
- but I don't think any sane person would used sed to do something like this. But for what it is worth, here is my sed example

ps -a -pid 1111 | sed -ne '/exmh/s/ tty.*$//'

Your best bet is probably to use the
ps -a -pid 1111 | grep exmh | cut -d' ' -f1
example. But rememeber that there is hours to fun to be had messing about with obscure programs on nix systems. :-)

regards

John Kelly
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#2 lore raw
In reply to Peter
Quoted 8 lines · Peter

Hi,

How do I isolate from a line like

1865 tty2 00:00:18 exmh

the first 4 numbers which is the PID. I get it with "ps -a --pid 1111 | grep exmh".

If your system has "pidof" installed; then

'pidof exmh'

Quoted 1 line

Thanks & regards

Signature · 15 lines
If the Linux community is a bunch of theives because they
try to imitate windows programs, then the Windows community
is built on organized crime.

Regards Richard
[email protected]
http://people.zeelandnet.nl/pa3gcu/



-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
author
#3 lore raw

Wonderful --who will ever fathom what hidden treasures there are in a Linux box.

I needed this PID of pppd for my little script which shows in a terminal From:
and Subject: as mail is coming in; tail -f --pid PID file.

Thanks again

Signature · 7 lines
Peter

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#4 lore raw
In reply to Peter

ps -a --pid 1111 | grep exmh | cut -b 2-5

HTH, Chuck

Quoted 8 lines · Peter

Hi,

How do I isolate from a line like

1865 tty2 00:00:18 exmh

the first 4 numbers which is the PID. I get it with

"ps -a --pid 1111 | grep > exmh".

                         ^
                         ?
Quoted 4 lines

Thanks & regards
--
Peter

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs