Re: Determine what called script (225 Views)
Reply
Occasional Advisor
Steve Givens
Posts: 14
Registered: ‎05-18-2004
Message 1 of 4 (225 Views)
Accepted Solution

Determine what called script

Is there a way to tell in a script whether it was called from the command line or by another script. And if it was a script, which one?

Thanks,

Steve
Honored Contributor
Patrick Wallek
Posts: 13,771
Registered: ‎06-21-2000
Message 2 of 4 (225 Views)

Re: Determine what called script

The easiest way is to look at the PPID (Parent Process ID) of the script. Then look for that PID. That will tell you what you want to know.

For example:

I want to know what started an instance of gpm.

gpm's PPID is 25067.

user 28888 25067 0 11:12:39 pts/0 1:08 gpm

So I look for PID 25067:

# ps -ef |grep 25067
user 25067 25065 0 11:10:45 pts/0 0:00 -sh
user 28888 25067 0 11:12:39 pts/0 1:08 gpm

You see that PID 25067 is a '-sh' process. So it was started from user's shell.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 3 of 4 (225 Views)

Re: Determine what called script

Hi Steve:

I don't know of any definitive way to determine this. A 'ps' of the running process can provide clues such as the parent process; the associated terminal (if any); etc. I'd start with :

# UNIX95= ps -eH

Regards!

...JRF...
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 4 of 4 (225 Views)

Re: Determine what called script

As mentioned, use the variable $PPID which is the process's parent. However, don't use grep and ps -- you'll get erratic results. The reason is that the PPID might be 123 and grep will match 123 1234 4123 24123 in both PID and PPID as well as possible command line parameters. Use this:

ps -fp $PPID

and you'll always get the parent's details.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.