Search code examples
linuxbashshutdown

Finding Which Program Runs Another


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.


Solution

  • 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).