问题
I have an NAS running on a limited version of what appears to be Redhat Linux. I followed directions to hack it so I could have shell access, which has been a big help. I've also made a couple modifications, ones that others have done and, other than one issue, they all seem to work fine.
Somehow, every 22 days, the system shuts down. I used a script running ps to find that shutdown is actually called, but I don't know what program calls shutdown.
If I rename /sbin/shutdown, then I could write a script to replace it. But the most important piece of information I'd like is what program is calling shutdown.
If a program runs my script (the phony /sbin/shutdown), how can I find out what program called my script? I want to be able to, from within the script, determine what program called the script in the first place. If it makes it easier, I can always use a Perl script instead of a bash script.
回答1:
In Bash, ps -p $$ -o ppid=
. The output is the pid of the parent process (the calling process). Having the parent pid, you can read its command line from /proc/<pid>/cmdline
(more on procfs).
回答2:
Easiest is probably just to dump the full output of ps afx
when your script gets called. That'll display a listing of all processes (including your script) in a tree format, showing not only what called your script, but what called that, and what else is running.
来源:https://stackoverflow.com/questions/10925429/finding-which-program-runs-another