Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to list all scripts (recursively) which are being called by a master script. For example:

master.sh

a.sh >> this script calls a1.sh, a2.sh, a3.sh etc
b.sh >> this one calls b1.sh, b2.sh, something.sh, etc.sh..
c.sh > similarly.

This is like the tree command for scripts.

The output could be like

master.sh
-a.sh
 |- a1.sh
    |-something.sh
    |-something_else.sh
-a2.sh
 |-x.sh
 |-y.sh
-a3.sh
...
share|improve this question
    
Thanks for formatting. –  Pandoo Jan 11 '13 at 18:22
    
If you really don't find anything else, you could always add things into your scripts (or making a copy) that say "if(you're being called by script:check_script_tree) { display the name of this script; call scripts; } else { do the script normally; }". A huge pain, but well, if you're desperate, you can do that. My teacher calls it making a program's skeleton. But there's probably something easier. –  Ariane Jan 11 '13 at 18:25
    
Yeah, I did not find anything else. It was mentioned somewhere to add "echo $0 > somefile.log" to all scripts, just to get a list of the files. That would be insane. –  Pandoo Jan 11 '13 at 18:30

1 Answer 1

up vote 1 down vote accepted

The best way to do this is pstree. It will list all running processes in a nice tree format. You can use the -H option to highlight a specific command (given as a PID). In the example below I am using pgrep to get the PID of master.sh :

pstree -H `pgrep master.sh` |grep -A 2 master.sh 
 |-bash---master.sh-+-a1.sh---something.sh
 |                  |-a2.sh
 |                  |-a3.sh

The ps command can also do what you need, the ps command can do everything except brew a good cup of coffee:

ps xjf

This will list all processes launched by master.sh (in fact, it will list all processes, including those launched by master.sh) in a tree-like format:

\_ master.sh
|   \_ a1.sh
|       \_ something.sh
|       \_ something.sh
|   \_ a2.sh
|   \_ a3.sh

I am only showing the relevant output here, the above command will also give you various other pieces of information such as the PID, process status etc etc.

share|improve this answer
    
Yeah it works. Wow. Thanks. –  Pandoo Jan 14 '13 at 18:42
    
Yup, the standard UNIX tools are impressive if cryptic. If you have nothing better to do over the next weekend and can't find "War and Peace", try reading through the 1230 lines of man ps :). –  terdon Jan 14 '13 at 18:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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