LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-01-2011, 03:50 PM   #1
nikunjbadjatya
Member
 
Registered: Sep 2009
Location: Bangalore
Posts: 33

Rep: Reputation: 0
Shell script calling shell script - List of all


Hi LQ members,

I am working on a build-test system of a set of codes.
The system ( large enough ) is such that one shell script does some work and calls another shell script.. this inturn calls a third shell script and so on..

My question is : How to get the list of all such shell scripts according to the way they are called. ? More like a shell script flow tree.!! ?

Ex.
Code:
#./1.sh 
   |
   `--> 2.sh
   |
   `--> 3.sh
        |
        `--> 4.sh
             |
             `--> 5.sh ....
                   .
                   .
                   .
etc.


Thanks,

Nikunj
Bangalore, India
 
Old 04-01-2011, 04:01 PM   #2
Cpare
Member
 
Registered: Aug 2001
Location: Magic City, USA
Distribution: Ubuntu
Posts: 65

Rep: Reputation: 15
Not sure how many shell scripts you’re talking about, but we do something similar where I work (shell scripts call each other like functions), to keep troubleshooting easier we add a "Echo Starting $0" to the start of every .sh, and an "Echo Exiting $0" at the end, this way we can quickly tell what is blowing up, and where we need to troubleshoot. There has been talk about having the ability add a 'debug' flag to each to enable/disable the output, but it's pretty low on the priority list.
 
Old 04-01-2011, 06:23 PM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by nikunjbadjatya View Post
My question is : How to get the list of all such shell scripts according to the way they are called. ? More like a shell script flow tree.!! ?
I guess the simplest option would be the command
Code:
ps axfu
but it outputs all processes as a process tree.

The do-it-yourself way would be to find out the PID of the original parent, e.g. via the process name, the use ps to list all its children recursively. Here is an example bash script; give it the name(s) of the parent script(s) as parameters:
Code:
#!/bin/bash
if [ $# -lt 1 ] || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
    echo "" >&2
    echo "Usage: $0 [ -h | --help ]" >&2
    echo "       $0 name-of-command-or-script .." >&2
    echo "" >&2
    exit 1
fi
pids=()
while [ $# -gt 0 ]; do
    if ! pids=(${pids[@]} `ps -C "$1" --no-headers -o pid`) ; then
        echo "$1: No such process." >&2
    fi
    shift 1
done
if [ ${#pids[@]} -lt 1 ]; then
    echo "No parent processes." >&2
    exit 1
fi

Processes () {
    local indent="$1"
    local separator="$2"
    shift 2

    while [ $# -gt 0 ]; do
        echo -n "$indent"
        ps --pid $1 --no-headers -o args
        Processes "$indent$separator" "$separator" `ps --ppid $1 --no-headers -o pid`
        shift 1
    done
}

Processes "" "    " ${pids[@]}
The actual work is done in the recursive Processes function. It uses ps --pid parent --no-headers -o pid to list the pids of all child processes (processes started by parent). You should be able to modify the output to suit your liking. Also, you can output various other information; the current one just prints the command line used to run the process. See man ps and STANDARD FORMAT SPECIFIERS section for the fields you can output via ps.

Hope this helps.
 
Old 04-09-2011, 03:30 AM   #4
nikunjbadjatya
Member
 
Registered: Sep 2009
Location: Bangalore
Posts: 33

Original Poster
Rep: Reputation: 0
Hi Nominal,
Thanks a lot for your script.
I created few sample scripts ( 1.sh --> 2.sh --> 3.sh --> 4.sh, 5.sh ) and put echo statements in them. Script 5.sh has a while infinite loop.

I tried running it as
Code:
[Terminal 1]
#./1.sh
...
...

[Terminal 2]
#./script_tree.sh 1.sh
1.sh: No such process.
No parent processes.
Where script_tree.sh is your script.!!

I checked with below command and found nothing.
Code:
#ps -ef | grep 1.sh
Any idea what is wrong here.?? I should be getting the tree.?

Thanks,
Nikunj
 
Old 04-09-2011, 05:14 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
hey, this software might just be exactly what you need

http://loader.sourceforge.net/
 
Old 04-09-2011, 12:14 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Please be aware that there are some different flavors of 'ps', each with their own options and output formatting. If you are running on a non-Linux host, this will probably have an impact, especially on something like Solaris. This particularly sucks, since there really is no standard API for acquiring information about the process table, and parsing the output of ps seems to be the standard method with the likes of the OP's problem. Nominal's use of very specific format specification is probably the best defense against the schizophrenic behavior of ps.

--- rod.
 
Old 04-13-2011, 10:09 AM   #7
nikunjbadjatya
Member
 
Registered: Sep 2009
Location: Bangalore
Posts: 33

Original Poster
Rep: Reputation: 0
Thanks for the useful information folks. !!
 
Old 04-13-2011, 07:27 PM   #8
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by nikunjbadjatya View Post
I tried running it as
Code:
[Terminal 1]
#./1.sh
...
...

[Terminal 2]
#./script_tree.sh 1.sh
1.sh: No such process.
No parent processes.
I cannot reproduce that on my machine (Ubuntu 10.10) using bash, dash or sh. Obviously, the script cannot find any running processes with 1.sh as the command, i.e. ps -C 1.sh returns no processes.

Also, if
Code:
ps x -o pid,args | grep -e 1.sh
returns no matches, then 1.sh has already exited and reaped.

If a process is no longer seen by ps, it is impossible to find which processes it started. (You can, however, use the Linux audit service to track program executions (exec system calls) and process creation (fork system calls), and afterwards look at the log to find out which processes spawned which.

My script only takes a snapshot of the current process relationships.

Quote:
Originally Posted by nikunjbadjatya View Post
I checked with below command and found nothing.
Code:
#ps -ef | grep 1.sh
Then 1.sh is no longer running, I'd say.

I tried with these four scripts, 1.sh to 4.sh:
Code:
#!/bin/sh
echo "1.sh ($$): Start"
./2.sh &
sleep 1
./3.sh &
sleep 1
./4.sh &
sleep 1
wait
echo "1.sh ($$): Exit"
Code:
#!/bin/sh
echo "2.sh ($$): Start"
./3.sh &
sleep 1
./4.sh &
sleep 1
wait
echo "2.sh ($$): Exit"
Code:
#!/bin/sh
echo "3.sh ($$): Start"
./4.sh &
sleep 1
wait
echo "3.sh ($$): Exit"
Code:
#!/bin/sh
echo "4.sh ($$): Start"
sleep 1
echo "4.sh ($$): Exit"
I started the above ./1.sh in one terminal, and in about a second, ran ./script_tree.sh 1.sh in another terminal. This is the result I get:
Code:
/bin/bash ./1.sh
    /bin/bash ./2.sh
        /bin/bash ./4.sh
            sleep 1
        sleep 1
    /bin/bash ./3.sh
        /bin/bash ./4.sh
            sleep 1
        sleep 1
    sleep 1
If you wait for too long, 1.sh exits, and you'll get the "1.sh: No such process.", "No parent processes." error message.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] calling vars in a for loop in a shell script januka Programming 9 10-11-2010 12:41 AM
Calling a bash shell script from within another script dedman Linux - Software 7 04-24-2010 09:53 PM
how to exit out of the calling Shell Script harimac Linux - Newbie 3 08-19-2009 02:49 AM
Calling PHP5 from a shell script Dafydd Programming 2 10-31-2008 08:31 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-12-2005 12:08 AM


All times are GMT -5. The time now is 11:39 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration