obtaining pid of child process

Viewed 40390

I am using python's multiprocessing module to spawn new process

as follows :

import multiprocessing
import os
d = multiprocessing.Process(target=os.system,args=('iostat 2 > a.txt',))
d.start()

I want to obtain pid of iostat command or the command executed using multiprocessing module

When I execute :

 d.pid 

it gives me pid of subshell in which this command is running .

Any help will be valuable .

Thanks in advance

5 Answers
[me@localhost ~]$ echo $$
30399
[me@localhost ~]$ cat iostat.py 
#!/usr/bin/env python3.4 

import multiprocessing
import os
d = multiprocessing.Process(target=os.system,args=('iostat 2 > a.txt',))
d.start()

[me@localhost ~]$ ./iostat.py &
[1] 31068
[me@localhost ~]$ watch -n 3 'pstree -p 30399'
[me@localhost ~]$ 

This gave me the PID of iostat See image. process tree

Related