Operating System - Linux
1752579 Members
2984 Online
108788 Solutions
New Discussion юеВ

Re: Determine what called script

 
SOLVED
Go to solution
Steve Givens
Occasional Advisor

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
3 REPLIES 3
Patrick Wallek
Honored Contributor

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.
James R. Ferguson
Acclaimed Contributor
Solution

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...
Bill Hassell
Honored Contributor

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.


Bill Hassell, sysadmin