Okay, so

I need to write a script which will take pid as an argument and give me the command name, the various network connections associated the process, and it will also give that information for up to three ancestor processes, and up to three generations of descendants.

I don't want the answer to this, but I do want some guidance on how best I can set myself on the right course to finishing this.

This is what I have started out with:

#get pid from user
#echo pid and find a way to describe process to user
echo $PID #whatever it is = #whatever the command name is
#network portion
sudo netstat -p grep $PID
#this return information related to the process itself
ps -ef f|egrep "(UID|$PID)"

The last line of code will at least return something somewhat valuable to me: I can get some information related to the process, a tree-like hierarchy, ect ect.

I don't posses a lot of technical knowledge so I understand if this code simply looks horrible/unusable for this assignment. If I need to start from scratch and come up with different ideas I will. Any help or guidance would be greatly appreciated.


You can get the PID of the parent process with this:

ps -o ppid --no-headers -p "$PID"

or with

awk '/^PPid:/ { print $2 }' /proc/$PID/status

You get it into a variable with

PPID="$(awk '/^PPid:/ { print $2 }' /proc/$PID/status)"
| improve this answer | |
  • Thank you! Can you can explain to me what this line of code is doing so that I may understand this a little better? – MrHal9000 Sep 16 '17 at 23:19
  • ps is used to print the process list. You can format the output. ps takes its data from the /proc filesystem. You get get the data from there directly, too. – Hauke Laging Sep 16 '17 at 23:25
  • Thanks Hauke; I'll keep adding on to it and see if I can do something awesome with this. – MrHal9000 Sep 16 '17 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.