I have a bash script which starts some processes in background, but when I do
ps aux | grep main_script.sh
ps shows more processes than I expect, so I'd like to further investigate/analyze what are those processes (sample output):
user 10001 ... /bin/bash main_script.sh
user 10002 ... /bin/bash main_script.sh
user 10003 ... /bin/bash main_script.sh
So, since all those pids are pointing to the parent script that started them as the command that's being ran (last part of ps output), I wonder if there is a way to take any of those process ids listed by ps, and get the info about what is actual command ran in the background, e.g.
if the main_script.sh contains this code:
./path/to/my/other/script.sh &
other_script_pid=$!
echo $other_script_pid
Which prints 10002, for example.
Is there any way I can get ./path/to/my/other/script.sh & given the pid 10002 , rather than its parent script main_script.sh?
Thanks in advice, any feedback if highly appreciated! :)
CodePudding user response:
If the sub-process isn't a daemon then the sub-process' parent process id should be the pid of your script. ps --ppid 10002
(with suitable flags to get the info of interest). They should also share the same session ID ps -s ...