How can I get only the ancestry processes of a given process?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
Is it correct that
pstree <pid>
will output all the descendant processes of the given processpstree -s <pid>
will output all the descendant processes and ancestry processes of the given process
How can I get only the ancestry processes of a given process?
Thanks.
process pstree
add a comment |Â
up vote
0
down vote
favorite
Is it correct that
pstree <pid>
will output all the descendant processes of the given processpstree -s <pid>
will output all the descendant processes and ancestry processes of the given process
How can I get only the ancestry processes of a given process?
Thanks.
process pstree
similar: View current user process ancestors and formating the output
â Stéphane Chazelas
Jul 28 at 8:56
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Is it correct that
pstree <pid>
will output all the descendant processes of the given processpstree -s <pid>
will output all the descendant processes and ancestry processes of the given process
How can I get only the ancestry processes of a given process?
Thanks.
process pstree
Is it correct that
pstree <pid>
will output all the descendant processes of the given processpstree -s <pid>
will output all the descendant processes and ancestry processes of the given process
How can I get only the ancestry processes of a given process?
Thanks.
process pstree
edited Jul 29 at 4:48
asked Jul 28 at 1:09
Tim
22.5k61222398
22.5k61222398
similar: View current user process ancestors and formating the output
â Stéphane Chazelas
Jul 28 at 8:56
add a comment |Â
similar: View current user process ancestors and formating the output
â Stéphane Chazelas
Jul 28 at 8:56
similar: View current user process ancestors and formating the output
â Stéphane Chazelas
Jul 28 at 8:56
similar: View current user process ancestors and formating the output
â Stéphane Chazelas
Jul 28 at 8:56
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
You can always walk the ancestry tree by hand using ps -o ppid=
:
#! /bin/bash -
pid=$1?Please give a pid
while
[ "$pid" -gt 0 ] &&
read -r ppid name < <(ps -o ppid= -o comm= -p "$pid")
do
printf '%sn' "$pid $name"
pid=$ppid
done
Or to avoid running ps
several times:
#! /bin/sh -
pid=$1?Please give a pid
ps -Ao pid= -o ppid= -o comm= |
awk -v p="$pid" '
pid = $1; ppid[pid] = $2
sub(/([[:space:]]*[[:digit:]]+)2[[:space:]]*/, "")
name[pid] = $0
END
while (p)
print p, name[p]
p = ppid[p]
'
Thanks. (1) Is there some reason to usebash
in the shebang of one script, andsh
in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/â¦
â Tim
Jul 29 at 17:43
@Tim, for (1) that's because<(...)
is not sh syntax. That will only work inzsh
,bash
or recent versions ofksh93
.... | read -r pid name
would only work inzsh
orksh93
, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
â Stéphane Chazelas
Jul 29 at 18:56
Thanks. (1) Can both scripts usebash
in shebang?
â Tim
Jul 29 at 19:01
add a comment |Â
up vote
2
down vote
You can try following, I found it in Linux man page :
-h
This highlight the current process and its ancestors.
-n
This will sort processes with the same ancestor by PID instead of by name.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You can always walk the ancestry tree by hand using ps -o ppid=
:
#! /bin/bash -
pid=$1?Please give a pid
while
[ "$pid" -gt 0 ] &&
read -r ppid name < <(ps -o ppid= -o comm= -p "$pid")
do
printf '%sn' "$pid $name"
pid=$ppid
done
Or to avoid running ps
several times:
#! /bin/sh -
pid=$1?Please give a pid
ps -Ao pid= -o ppid= -o comm= |
awk -v p="$pid" '
pid = $1; ppid[pid] = $2
sub(/([[:space:]]*[[:digit:]]+)2[[:space:]]*/, "")
name[pid] = $0
END
while (p)
print p, name[p]
p = ppid[p]
'
Thanks. (1) Is there some reason to usebash
in the shebang of one script, andsh
in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/â¦
â Tim
Jul 29 at 17:43
@Tim, for (1) that's because<(...)
is not sh syntax. That will only work inzsh
,bash
or recent versions ofksh93
.... | read -r pid name
would only work inzsh
orksh93
, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
â Stéphane Chazelas
Jul 29 at 18:56
Thanks. (1) Can both scripts usebash
in shebang?
â Tim
Jul 29 at 19:01
add a comment |Â
up vote
4
down vote
accepted
You can always walk the ancestry tree by hand using ps -o ppid=
:
#! /bin/bash -
pid=$1?Please give a pid
while
[ "$pid" -gt 0 ] &&
read -r ppid name < <(ps -o ppid= -o comm= -p "$pid")
do
printf '%sn' "$pid $name"
pid=$ppid
done
Or to avoid running ps
several times:
#! /bin/sh -
pid=$1?Please give a pid
ps -Ao pid= -o ppid= -o comm= |
awk -v p="$pid" '
pid = $1; ppid[pid] = $2
sub(/([[:space:]]*[[:digit:]]+)2[[:space:]]*/, "")
name[pid] = $0
END
while (p)
print p, name[p]
p = ppid[p]
'
Thanks. (1) Is there some reason to usebash
in the shebang of one script, andsh
in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/â¦
â Tim
Jul 29 at 17:43
@Tim, for (1) that's because<(...)
is not sh syntax. That will only work inzsh
,bash
or recent versions ofksh93
.... | read -r pid name
would only work inzsh
orksh93
, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
â Stéphane Chazelas
Jul 29 at 18:56
Thanks. (1) Can both scripts usebash
in shebang?
â Tim
Jul 29 at 19:01
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You can always walk the ancestry tree by hand using ps -o ppid=
:
#! /bin/bash -
pid=$1?Please give a pid
while
[ "$pid" -gt 0 ] &&
read -r ppid name < <(ps -o ppid= -o comm= -p "$pid")
do
printf '%sn' "$pid $name"
pid=$ppid
done
Or to avoid running ps
several times:
#! /bin/sh -
pid=$1?Please give a pid
ps -Ao pid= -o ppid= -o comm= |
awk -v p="$pid" '
pid = $1; ppid[pid] = $2
sub(/([[:space:]]*[[:digit:]]+)2[[:space:]]*/, "")
name[pid] = $0
END
while (p)
print p, name[p]
p = ppid[p]
'
You can always walk the ancestry tree by hand using ps -o ppid=
:
#! /bin/bash -
pid=$1?Please give a pid
while
[ "$pid" -gt 0 ] &&
read -r ppid name < <(ps -o ppid= -o comm= -p "$pid")
do
printf '%sn' "$pid $name"
pid=$ppid
done
Or to avoid running ps
several times:
#! /bin/sh -
pid=$1?Please give a pid
ps -Ao pid= -o ppid= -o comm= |
awk -v p="$pid" '
pid = $1; ppid[pid] = $2
sub(/([[:space:]]*[[:digit:]]+)2[[:space:]]*/, "")
name[pid] = $0
END
while (p)
print p, name[p]
p = ppid[p]
'
edited Jul 29 at 9:33
answered Jul 28 at 8:06
Stéphane Chazelas
278k52511841
278k52511841
Thanks. (1) Is there some reason to usebash
in the shebang of one script, andsh
in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/â¦
â Tim
Jul 29 at 17:43
@Tim, for (1) that's because<(...)
is not sh syntax. That will only work inzsh
,bash
or recent versions ofksh93
.... | read -r pid name
would only work inzsh
orksh93
, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
â Stéphane Chazelas
Jul 29 at 18:56
Thanks. (1) Can both scripts usebash
in shebang?
â Tim
Jul 29 at 19:01
add a comment |Â
Thanks. (1) Is there some reason to usebash
in the shebang of one script, andsh
in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/â¦
â Tim
Jul 29 at 17:43
@Tim, for (1) that's because<(...)
is not sh syntax. That will only work inzsh
,bash
or recent versions ofksh93
.... | read -r pid name
would only work inzsh
orksh93
, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?
â Stéphane Chazelas
Jul 29 at 18:56
Thanks. (1) Can both scripts usebash
in shebang?
â Tim
Jul 29 at 19:01
Thanks. (1) Is there some reason to use
bash
in the shebang of one script, and sh
in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/â¦â Tim
Jul 29 at 17:43
Thanks. (1) Is there some reason to use
bash
in the shebang of one script, and sh
in the other? (2) Is there some difference between double dash and single dash unix.stackexchange.com/questions/459208/â¦â Tim
Jul 29 at 17:43
@Tim, for (1) that's because
<(...)
is not sh syntax. That will only work in zsh
, bash
or recent versions of ksh93
. ... | read -r pid name
would only work in zsh
or ksh93
, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?â Stéphane Chazelas
Jul 29 at 18:56
@Tim, for (1) that's because
<(...)
is not sh syntax. That will only work in zsh
, bash
or recent versions of ksh93
. ... | read -r pid name
would only work in zsh
or ksh93
, here-doc+cmdsubst would be sh but more convoluted and less efficient. For (2), see Why the "-" in the "#! /bin/sh -" shebang?â Stéphane Chazelas
Jul 29 at 18:56
Thanks. (1) Can both scripts use
bash
in shebang?â Tim
Jul 29 at 19:01
Thanks. (1) Can both scripts use
bash
in shebang?â Tim
Jul 29 at 19:01
add a comment |Â
up vote
2
down vote
You can try following, I found it in Linux man page :
-h
This highlight the current process and its ancestors.
-n
This will sort processes with the same ancestor by PID instead of by name.
add a comment |Â
up vote
2
down vote
You can try following, I found it in Linux man page :
-h
This highlight the current process and its ancestors.
-n
This will sort processes with the same ancestor by PID instead of by name.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can try following, I found it in Linux man page :
-h
This highlight the current process and its ancestors.
-n
This will sort processes with the same ancestor by PID instead of by name.
You can try following, I found it in Linux man page :
-h
This highlight the current process and its ancestors.
-n
This will sort processes with the same ancestor by PID instead of by name.
answered Jul 28 at 3:30
Nur
214
214
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f458973%2fhow-can-i-get-only-the-ancestry-processes-of-a-given-process%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
similar: View current user process ancestors and formating the output
â Stéphane Chazelas
Jul 28 at 8:56