Operating System - HP-UX
1822512 Members
2513 Online
109642 Solutions
New Discussion

How to get the name of the calling script or binary from a ksh script ?

 
SwissKnife
Frequent Advisor

How to get the name of the calling script or binary from a ksh script ?

Hi,

 

How to use $PPID value to get the name of the parent process calling my script.

I tried this:

parent=$(ps -f -p $PPID | tail -1 | awk '{print $NF}')

echo $parent

 

but output is not efficient.

-ksh

 

Any ideas ?

 

Kind regards, Den.

9 REPLIES 9
Steven Schweda
Honored Contributor

Re: How to get the name of the calling script or binary from a ksh script ?

> How to use $PPID value to get the name of the parent process calling
> my script.

   Define "name of process".  Does a UNIX process have a name?  On VMS,
a process does have a name.  For example:

alp $ show system /process = bat*
OpenVMS V8.4  on node ALP   24-FEB-2017 06:42:43.34   Uptime  86 06:36:52
  Pid    Process Name    State  Pri      I/O       CPU       Page flts  Pages
2020ECCD BATCH_590       LEF      5      189   0 00:00:00.14       182    138  B

   But, on VMS, it's harder to get the name of the command (program with
arguments) being run in a process, like UNIX "ps" output.

> but output is not efficient.

   I don't understand.  What, exactly, is the problem?


   Why do you care?  What, exactly, is the problem which you are trying
to solve?

SwissKnife
Frequent Advisor

Re: How to get the name of the calling script or binary from a ksh script ?

Hi,

Thanks. I have scripts calling other scripts.I want to know what is the parent script name.

 

example:

aa.ksh calls bb.ksh

zz.ksh calls bb.ksh

 

When bb.ksh is running, is there a way to give the name of script doing the call ?

 

Kind regards,

Den

 

 

Re: How to get the name of the calling script or binary from a ksh script ?

2 things to do here

1. if your scripts are all ksh scripts make sure the first line of all the scripts is

 

#!/usr/bin/ksh

2.   Try using the following code instead:

UNIX95= ps -p $PPID -o args=

I am an HPE Employee
Accept or Kudo
Steven Schweda
Honored Contributor

Re: How to get the name of the calling script or binary from a ksh script ?

> When bb.ksh is running, is there a way to give the name of script
> doing the call ?

   _Which_ script doing _which_ call?  Doesn't your "ps" command do
that?  I'm confused.

   Or do you see the problem with your "ps" command if a script has
command-line arguments (as in: "bb.sh x y z")?

   The easy way to do this might be to have the top-level script set
some environment variable, which any/every sub-process can see.
(Depending on exactly what you really want, of course, which is still
unclear to me.)

> aa.ksh calls bb.ksh
> zz.ksh calls bb.ksh

   And what output do you want in each case?

SwissKnife
Frequent Advisor

Re: How to get the name of the calling script or binary from a ksh script ?

Hi

 

> aa.ksh calls bb.ksh

output from bb.ksh will say: aa.ksh has called me


> zz.ksh calls bb.ksh

output from bb.ksh will say: zz.ksh has called me

 

Kind regards, Den.

SwissKnife
Frequent Advisor

Re: How to get the name of the calling script or binary from a ksh script ?

Hi,

I give a Kudo but it seems not working

...

delphix:/tmp#cat bb.ksh
#!/usr/bin/ksh
parent=`ps -p $PPID -o args=`
echo $parent

UNIX95= ps -p $PPID -o args=
echo $UNIX95
delphix:/tmp#
delphix:/tmp#cat aa.ksh
./bb.ksh

 

...

 

 

execution:

 


delphix:/tmp#./aa.ksh
-ksh
-ksh

 

Kind regards,

Den.

 

Dennis Handly
Acclaimed Contributor

Re: How to get the name of the calling script or binary from a ksh script ?

>  Define "name of process".  Does a UNIX process have a name?

 

The basename of the executable is as good as any.

 

>parent=`ps -p $PPID -o args=`

 

The "proper" syntax is:

parent=$(UNIX95=EXTENDED_PS ps -p $PPID -oargs=)

If the caller is a shell, you'll see the name of the shell then the script and args:

/usr/bin/ksh ./yyyy.sh

If not a shell, then -ocomm= would be good enough.

 

>echo $UNIX95

 

This is useless since the variable is only set for that process.  Also when you are echoing a bunch of things, it helps to have a title:

echo "UNIX95: $UNIX95"

SwissKnife
Frequent Advisor

Re: How to get the name of the calling script or binary from a ksh script ?

It does not work:

 

R018366[root]/tmp # cat aa.ksh
./bb.ksh
SR018366[root]/tmp # cat bb.ksh
parent=$(UNIX95=EXTENDED_PS ps -p $PPID -oargs=)
echo $parent
echo $UNIX95



parent=$(UNIX95=EXTENDED_PS ps -p $PPID -ocomm=)
echo $parent
echo $UNIX95

 

 

I run

./aa.ksh

Output

SR018366[root]/tmp # ./aa.ksh
-ksh

ksh

 

 

Not better !

Dennis Handly
Acclaimed Contributor

Re: How to get the name of the calling script or binary from a ksh script ?

> It does not work:

 

You didn't do what Duncan said and I always do.  You MUST start each script with the name of the interpreter:

#!/usr/bin/ksh

Not sure why it makes a difference but the shell doesn't know the name of the script if you forget it.

 

> echo $UNIX95

 

As I said, this does nothing, remove it.

 

>-ksh
>ksh

 

These are the actual names of the parent process.  The login and subshell ksh.