in reply to How to Find the Script that Called it

Assuming a POSIX system, you can't.

All you can do is get the parent process id and if that process is still running find out more. And most of more is probably system dependent.

Is there any sane reason you can't pass an argument or environment variable and use the (lack off) that to differentiate the behaviour? It would probably help testing too.

  • Comment on Re: How to Find the Script that Called it

Replies are listed 'Best First'.
Re^2: How to Find the Script that Called it
by bichonfrise74 (Vicar) on Dec 18, 2008 at 01:16 UTC
    The reason is that there are about 10 scripts that is calling the main script. So, if I try to pass an argument, then it means I have to change the 10 scripts on how it is calling the main script.

    I certainly can do that but it means I have to test the 10 scripts...
      You can set an environment variable in the one parent script that you're looking for. E.g. $ENV{RUNNING_FROM_FOO}=1; in foo.pl, then in bar.pl test if ($ENV{RUNNING_FROM_FOO}) { ... }.

      Update: I see the Joost already suggested an environment variable, but you seem to have missed it (as did I). So it's probably worth saying again anyway :-)

        Thank you all for your suggestions.