Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ps -o pid,ppid,pgid,gid,sess --ppid $$ 2>&1 | awk '1'

15 views
Skip to first unread message

Hongyi Zhao

unread,
Apr 12, 2020, 2:47:45 AM4/12/20
to
Hi,

See the following test:

$ ps -o pid,ppid,pgid,gid,sess --ppid $$ | awk '1'
PID PPID PGID GID SESS
7175 6234 7175 1000 6234
7176 6234 7175 1000 6234

I've already redirected both stdout and stderr to the pipe, why the results will be printed twice?

Regards

Hongyi Zhao

unread,
Apr 12, 2020, 2:49:24 AM4/12/20
to
在 2020年4月12日星期日 UTC+8下午2:47:45,Hongyi Zhao写道:
> Hi,
>
> See the following test:
>
> $ ps -o pid,ppid,pgid,gid,sess --ppid $$ | awk '1'

My typo, I mean the following:

$ ps -o pid,ppid,pgid,gid,sess --ppid $$ 2>&1 | awk '1'
PID PPID PGID GID SESS
24092 21819 24092 1000 21819
24093 21819 24092 1000 21819

Keith Thompson

unread,
Apr 12, 2020, 6:11:28 AM4/12/20
to
Hongyi Zhao <hongy...@gmail.com> writes:
> 在 2020年4月12日星期日 UTC+8下午2:47:45,Hongyi Zhao写道:
>> See the following test:
>>
>> $ ps -o pid,ppid,pgid,gid,sess --ppid $$ | awk '1'
>
> My typo, I mean the following:
>
> $ ps -o pid,ppid,pgid,gid,sess --ppid $$ 2>&1 | awk '1'
> PID PPID PGID GID SESS
> 24092 21819 24092 1000 21819
> 24093 21819 24092 1000 21819
>>
>> I've already redirected both stdout and stderr to the pipe, why the
>> results will be printed twice?

It's not printing the same thing twice. Those are two different PIDs.

Adding ",cmd" shows what's going on:

$ ps -o pid,ppid,pgid,gid,sess,cmd --ppid $$ 2>&1 | awk '1'
PID PPID PGID GID SESS CMD
18708 10491 18708 1000 10491 ps -o pid,ppid,pgid,gid,sess,cmd --ppid 10491
18709 10491 18708 1000 10491 awk 1
$

It's printing lines for the ps process and the awk process, both of
which share the same PPID (namely $$, your current shell process).

In general, when you're trying to solve something like this, think about
adding more information to the output. The raw numeric values were not
terribly useful.

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Hongyi Zhao

unread,
Apr 12, 2020, 10:57:22 AM4/12/20
to
在 2020年4月12日星期日 UTC+8下午6:11:28,Keith Thompson写道:
> Hongyi Zhao <hongy...@gmail.com> writes:
> > 在 2020年4月12日星期日 UTC+8下午2:47:45,Hongyi Zhao写道:
> >> See the following test:
> >>
> >> $ ps -o pid,ppid,pgid,gid,sess --ppid $$ | awk '1'
> >
> > My typo, I mean the following:
> >
> > $ ps -o pid,ppid,pgid,gid,sess --ppid $$ 2>&1 | awk '1'
> > PID PPID PGID GID SESS
> > 24092 21819 24092 1000 21819
> > 24093 21819 24092 1000 21819
> >>
> >> I've already redirected both stdout and stderr to the pipe, why the
> >> results will be printed twice?
>
> It's not printing the same thing twice. Those are two different PIDs.
>
> Adding ",cmd" shows what's going on:
>
> $ ps -o pid,ppid,pgid,gid,sess,cmd --ppid $$ 2>&1 | awk '1'
> PID PPID PGID GID SESS CMD
> 18708 10491 18708 1000 10491 ps -o pid,ppid,pgid,gid,sess,cmd --ppid 10491
> 18709 10491 18708 1000 10491 awk 1
> $
>
> It's printing lines for the ps process and the awk process, both of
> which share the same PPID (namely $$, your current shell process).
>
> In general, when you're trying to solve something like this, think about
> adding more information to the output. The raw numeric values were not
> terribly useful.

Thanks a lot.
0 new messages