Skip to contentSkip to conversation

/proc/${PID}/cmdline parsing

DISCUSSION Mihamina Rakotomandimby 4 participants 6 messages Jun 3, 2010 – Jun 5, 2010

lore
Share & tools
lore
#0 lore raw

Manao ahoana, Hello, Bonjour,

If I launch a

 $ watch find / -type f -name 'toto'
(it's a useless command just for the example)
and then I get the PID of this.

When I

 $ cat /proc/${PID}/cmdline
 watchfind/-typef-nametoto

Yes, it's the command line, but I would like to parse it, in order to display the launched executable + its arguments.

How to parse it?

Misaotra, Thanks, Merci.

Signature · 8 lines
       Architecte Informatique chez Blueline/Gulfsat:
    Administration Systeme, Recherche & Developpement
                                     +261 3456 000 19
--
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 6 lines · Mihamina Rakotomandimby

Manao ahoana, Hello, Bonjour,

If I launch a

 $ watch find / -type f -name 'toto'
(it's a useless command just for the example)
and then I get the PID of this.

PID of what? watch or find?

Quoted 10 lines

When I

 $ cat /proc/${PID}/cmdline
 watchfind/-typef-nametoto

Yes, it's the command line, but I would like to parse it, in order to display the launched executable + its arguments.

How to parse it?

Could you be a bit more clear with your question?

take care,
Joel

--
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
Quoted 6 lines

Joel Fernandes <[email protected]> :

Quoted 4 lines

If I launch a

 $ watch find / -type f -name 'toto'
(it's a useless command just for the example)
and then I get the PID of this.

PID of what? watch or find?

PID of "watch"

Quoted 7 lines
Quoted 6 lines

When I

 $ cat /proc/${PID}/cmdline
 watchfind/-typef-nametoto
Yes, it's the command line, but I would like to parse it, in order to
display the launched executable + its arguments.
How to parse it?

Could you be a bit more clear with your question?

I want to split "watchfind/-typef-nametoto" (the content
of /proc/${PID}/cmdline) to get "watch find / -type f -name toto" (the command I issued above)

But I cannot figure out how to split it.
Would you know another place in the system I should look at?

Signature · 8 lines
       Architecte Informatique chez Blueline/Gulfsat:
    Administration Systeme, Recherche & Developpement
                                     +261 3456 000 19
--
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
#3 lore raw
Quoted 15 lines · Mihamina Rakotomandimby
Quoted 6 lines

Joel Fernandes <[email protected]> :

Quoted 4 lines

If I launch a

 $ watch find / -type f -name 'toto'
(it's a useless command just for the example)
and then I get the PID of this.

PID of what? watch or find?

PID of "watch"

Quoted 6 lines
Quoted 6 lines

When I

 $ cat /proc/${PID}/cmdline
 watchfind/-typef-nametoto
Yes, it's the command line, but I would like to parse it, in order to
display the launched executable + its arguments.
How to parse it?
Quoted 9 lines
Quoted 1 line

Could you be a bit more clear with your question?

I want to split "watchfind/-typef-nametoto" (the content
of /proc/${PID}/cmdline) to get "watch find / -type f -name toto" (the command I issued above)

But I cannot figure out how to split it.
Would you know another place in the system I should look at?

Running 'hexdump -C /proc/2202/cmdline' produces this:

00000000  2f 73 62 69 6e 2f 67 65  74 74 79 00 33 38 34 30 |/sbin/getty.3840|
00000010  30 00 74 74 79 31 00                             |0.tty1.|

00000017

It looks like the fields are separated by 0 bytes. You'll simply need to read the file and split on every zero byte.

Kristof

--
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
Quoted 41 lines · Kristof Provost
Quoted 19 lines · Mihamina Rakotomandimby
Quoted 9 lines

Joel Fernandes <[email protected]> :

Quoted 5 lines

If I launch a

 $ watch find / -type f -name 'toto'
(it's a useless command just for the example)
and then I get the PID of this.

PID of what? watch or find?

PID of "watch"

Quoted 7 lines
Quoted 7 lines

When I

 $ cat /proc/${PID}/cmdline
 watchfind/-typef-nametoto
Yes, it's the command line, but I would like to parse it, in order to
display the launched executable + its arguments.
How to parse it?
Quoted 10 lines
Quoted 2 lines

Could you be a bit more clear with your question?

I want to split "watchfind/-typef-nametoto" (the content
of /proc/${PID}/cmdline) to get "watch find / -type f -name toto" (the command I issued above)

But I cannot figure out how to split it.
Would you know another place in the system I should look at?

Running 'hexdump -C /proc/2202/cmdline' produces this:

00000000  2f 73 62 69 6e 2f 67 65  74 74 79 00 33 38 34 30 |/sbin/getty.3840|
00000010  30 00 74 74 79 31 00                             |0.tty1.|

00000017

It looks like the fields are separated by 0 bytes. You'll simply need to read the file and split on every zero byte.

Quoted 8 lines

Kristof

--
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

You're right.

I'm no awk expert, so the following command might not be as beautiful as can be, but it does exactly what Mihamina wants :

$ cat /proc/PID/cmdline | awk '{BEGIN {FS = "\0"} ; {ORS = " "}; {for (i = 1 i < NF; i++) print $i}'

Cyril.
--
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

#5 lore raw
In reply to Kristof Provost
Quoted 19 lines
Quoted 11 lines
Quoted 8 lines

I want to split "watchfind/-typef-nametoto" (the content
of /proc/${PID}/cmdline) to get "watch find / -type f -name toto" (the command I issued above)

But I cannot figure out how to split it.
Would you know another place in the system I should look at?

Running 'hexdump -C /proc/2202/cmdline' produces this:

Quoted 7 lines

00000000  2f 73 62 69 6e 2f 67 65  74 74 79 00 33 38 34 30

|/sbin/getty.3840|
00000010  30 00 74 74 79 31 00                             |0.tty1.|

00000017

It looks like the fields are separated by 0 bytes. You'll simply need to read the file and split on every zero byte.

Quoted 8 lines

You're right.

I'm no awk expert, so the following command might not be as beautiful as can be, but it does exactly what Mihamina wants :

$ cat /proc/PID/cmdline | awk '{BEGIN {FS = "\0"} ; {ORS = " "}; {for (i = 1
i < NF; i++) print $i}'

While we're at it, here's a perl one-liner :-)
cat /proc/self/cmdline | perl -F"\0" -lane 'foreach(@F) { print; }'

take care,
Joel
--
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