Score:0

Writing a script that takes PID as an argument and prints PID's of all its GRANDchildren and grandgrandchildren

pr flag

Hello I am trying to write a script that takes an argument of a PID, and prints all the PID's grandchildren and grand-grandchildren, not the children. example:

$ printthem 3545
3546
3548
3666

What I tried to do :

ps --ppid $1

But it doesn't give me the ID. Also I've read somewhere about pstree, but I am not quite sure how to use it properly to achieve my goal. Any guidance will be much appriciated. Thanks !

Nitsan Asraf avatar
pr flag
@bac0n thanks alot !
BeastOfCaerbannog avatar
ca flag
@24601 The OP wants to know the PIDs of programs that are probably run on Ubuntu. Furthermore, they are asking for a Bash-related solution, judging by the tags they used. Since Bash is a big part of Ubuntu, Bash questions are on-topic and well supporrted here. So the question is perfectly on-topic and should not be closed. Also read this [meta post](https://meta.askubuntu.com/questions/13807/are-bash-shell-scripting-questions-on-topic) about Bash-related questions.
24601 avatar
in flag
@BeastOfCaerbannog where does the OP say that they are using ubuntu. Generic questions about scripting can be asked on[stackoverflow](https://meta.stackoverflow.com/questions/340130/where-to-ask-the-simplest-programming-questions)
Nitsan Asraf avatar
pr flag
@24601 Well I am asking it in AskUbuntu so you can assume I am using Ubuntu. If you are not sure, you can ask instead of assuming it's not related.
BeastOfCaerbannog avatar
ca flag
@24601 Well, not explicitly mentioning that they use Ubuntu is not a strong-enough reason for considering a question as off-topic. It's true that Bash questions can be asked in SO too, but they can be also asked here (and Unix & Linux and Super User too!). We even have a badge for Bash questions. As you can see, bacon's comment provides a fine answer, perfectly valid for Ubuntu, exactly as the question, even without explicitly mentioning Ubuntu.
24601 avatar
in flag
@BeastOfCaerbannog I'm not intending to argue the point except so as to say that where the question is specific to the OS then of course it is on topic otherwise generic questions which are not ubuntu OS specific can be dealt with quite adequately in stackoverflow. The fact someone answers in a comment is not proof alone that the Q is correctly entered in AU. If you have a problem with that then raise it in meta - which is the correct place to do so.
BeastOfCaerbannog avatar
ca flag
@24601 Well, the meta post I linked in my previous comment, which I will also link again here, clarifies that general scripting questions, especially Bash-scripting, are not off-topic here. Meta post: [Are bash/shell scripting questions on topic?](https://meta.askubuntu.com/questions/13807/are-bash-shell-scripting-questions-on-topic)
BeastOfCaerbannog avatar
ca flag
@24601 Also relevant: [How do we tell if a question belongs here, or rather at stackoverflow/superuser?](https://meta.askubuntu.com/questions/47/how-do-we-tell-if-a-question-belongs-here-or-rather-at-stackoverflow-superuser) and [Are “not only Ubuntu-specific” questions on-topic?](https://meta.askubuntu.com/questions/14523/are-not-only-ubuntu-specific-questions-on-topic)
24601 avatar
in flag
@BeastOfCaerbannog I've not changed my view based on that reference, sorry.
terdon avatar
cn flag
@24601 The scope of this site is _anything that can be done on an Ubuntu machine_. This is 100% on topic. Generic questions that are not Ubuntu-specific form the vast majority of what we deal with here. There are very, very few things that are actually Ubuntu-specific. Also, most, including this question, would not be on topic on [so] which only deals with programming questions. Please take the time to understand the site's scope before suggesting things are off topic.
waltinator avatar
it flag
Did you read `man ps`? Where did you get `--ppid` from? `ps -o PPID $1` to get `$1`'s parent.
A. Herlas avatar
bz flag
you are looking for `pstree -p $pid`, then you have to figure out how to extract the info.
Score:1
it flag

Here's a meta-language design(not implemented,not tested, must be assumed to not work). Actual coding is left as an exercise for the student.

In a bashscript (beginning with #!/bin/bash):

Define a ppid function taking a PID as a parameter, and returning the PID's parent.

Define a children function taking a PID as a parameter, and returning a space-seperated list ("pid pid pid " note the trailing space) of PIDs that have the parameter PID as a parent. Check ALL the PIDs on the system, using the ppid function.

startPID=$1
childlist="$(children $startPID)"
gchildlist=""
ggchildlist=""
for kid in $childlist ; do
    gchildlist="$gchildlist $(children $kid)"
done
for kid in $gchildlist ; do
    ggchildlist="$ggchildlist $(children $kid)"
done
echo "Grand :  $gchildlist"
echo "Ggrand: $ggchildlist"
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.