tags:

views:

153

answers:

6

Any way for a perl script to know who called it and/or how?

Be it another script, or an executable. Straight from the command-line or the cron scheduler.

+2  A: 

On linux you can look at /proc/<PID>/ for information, it should give you the exact command used to start it, and the user as well.

webdestroya
+1  A: 

How about: ps auxww

There you will see the username + parameters.

Eduardo
+2  A: 

You can use CORE::getppid() to find the id of the parent process.

Evan Carroll
+2  A: 

Calling getppid will give you the parent process of the script, and that's the process that launched your script. You can then use that to interrogate the process table to determine what that process is.

Brian Agnew
Downvoted why ?
Brian Agnew
+16  A: 

Tools to help track down how a Perl script was started:

getppid returns parent process id. You can then use ps or /proc/<pid> to get more information about the calling process.

$^X: full path to the perl interpreter, which may provide a clue about how Perl was started from the shell

$0, __FILE__: name of the script invoked from the command line, and the current file name. If they are consistent, then the current file contains the script that was invoked from the command line.

@ARGV: command line arguments passed to your script. With $^X, $0, and @ARGV, you know exactly how the Perl interpreter was started from the shell.

caller: stack trace information. If caller returns undef at the start of a script, then you are at the top frame of the stack, and your script was invoked from the shell. Otherwise caller returns the package, file, and line where your script was invoked (with do or require).

$^T: time (in seconds since the "epoch") that the current Perl script was started, so you know when the current Perl interpreter was started from the shell. Use scalar localtime($^T) to see this value in a friendlier format.

mobrule
+1 Great post would read again! AAAAAA++++
friedo
@friedo: WHOA THERE.
jrockway
Brian D Foy's modulino approach, which normally uses the caller function to allow a script to be used as a module, may also be of interest. Combined with the other variables and functions mentioned by mobrule, you can write some very flexible scripts/modules.http://www252.pair.com/comdog/mastering_perl/Chapters/18.modulinos.html
Matthew S
+6  A: 

Aviatrix deleted an answer where he said, "Try using command-line arguments". This is a good idea. Programs that depend on things that can't be controlled are difficult to test. As you develop your script, do you really want to run it from cron to test the "being run from cron" functionality? No way!

So instead of making your script guess what it's being run from, just tell it. Then it won't have to guess, and then it won't guess incorrectly (at three in the morning, no doubt).

Reliable software is a pure function from its input to its output. Don't add impurity without an excellent reason. ("I want it to randomly be different when run from cron" is not a good reason.)

jrockway
Upvote for programmer education.
daxim