I was wondering if there is a way to trace where a command was run from in Linux.
For example, if I call a script, is there any way to trace where it was called from, like started from a program or another bash script.
|
I was wondering if there is a way to trace where a command was run from in Linux. For example, if I call a script, is there any way to trace where it was called from, like started from a program or another bash script. | |||
|
feedback
|
|
If your script is written is bash, there is a built-in variable called $PPID (short for parent's process ID, I guess) that you can use to find out who called your script. Here is an example:
Explanation:
| |||
|
feedback
|
|
"Was run from" is rather difficult I would think unless there is some logging in the executable that was run. If the execution is finished there's probably not much left to look into. If it is being executed, you might be able to get enough information on the caller from the parent process id(PPID) by simply using | |||
|
feedback
|
|
If the process is still running then you can use the hierarchical visualization (called also forest or process tree) offered by the Try for example:
And the output will look like:
| |||
|
feedback
|