How to find out how a perl script was run?

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

Will it be another script or executable. Directly from the command line or cron scheduler.

+4
source share
6 answers

Tools to help track how the Perl script was run:

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

$^X : the full path to the perl interpreter, which can give an idea of ​​how Perl was launched from the shell

$0 , __FILE__ : the script name is called from the command line and the current file name. If they are consistent, then the current file contains a script that was called 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 launched from the shell.

caller : stack trace information. If caller returns undef at the beginning of the script, then you are in the top frame of the stack and your script has been called from the shell. Otherwise, caller returns the package, file, and line where your script was called (using do or require ).

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

+16
source

Aviatrix deleted the answer, where he said: "Try using command line arguments." This is a good idea. Programs that depend on things that cannot be controlled are difficult to verify. When you develop your script, do you really want to run it from cron in order to test the "be run from cron" functionality? In no case!

So, instead of making your script guess what it starts from, just say it. Then he will not have to guess, and then he will not guess correctly (at three in the morning, no doubt).

Reliable software is a pure function from input to output. Do not add impurities without a good reason. ("I want it to randomly differ on startup from cron," is not a good reason.)

+6
source

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

+3
source

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

+2
source

A getppid call will give you the parent script process and the process that launched your script. You can then use this to poll the process table to determine what kind of process it is.

+2
source

How about: ps auxww

There you will see the username + parameters.

+1
source

Source: https://habr.com/ru/post/1309050/


All Articles