Finding Which Program Runs Another

社会主义新天地 提交于 2019-12-08 02:34:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!