Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know the parent process if the child is launched in background and the parent is already exited

I have a child process running in foreground and his father is already exited.

the proc/$pid/stat file does not contain any more the parent pid if the parent is already exited and it display 1 instead of the origin parent pid

linux$cat /proc/6267/stat
6267 (test3.sh) S 1 6265 ......
#                 ^
#                 |
#        I expected to get the origin parent pid but I get 1

To reproduce this behaviour quickly we can use the following scripts

test2.sh

#!/bin/sh
echo "test2=$$"
./test3.sh &

test3.sh

#!/bin/sh
echo "test3=$$"
sleep 1000

execution:

linux$ ./test2.sh
test2=6318
test3=6319
linux$ ps aux | grep test
 6319 root      1484 S    {test3.sh} /bin/sh ./test3.sh
linux$ cat /proc/6319/stat
6319 (test3.sh) S 1 6318 2138 34816 6.......
like image 422
MOHAMED Avatar asked Nov 11 '22 22:11

MOHAMED


1 Answers

I don't think you can know the parent of a process after the (parent) has finished executing. But maybe you can have another program running which monitors your original program and maintains a log file or something. And orphaned processes are of course, adopted by init.

like image 53
Hrishi Avatar answered Nov 14 '22 22:11

Hrishi