Search code examples
pythonpidpexpect

How do I get the pid of a spawned process using pexpect?


I am using pexpect to spawn a bash shell and then using sendline to execute other processes within the bash shell.

Is there any way to get the pid of the spawned bash shell?
How can I get the pid of the process that I have started within bash?


Solution

  • If you check the documentation of the spawn class you'll find that you can get the pid of the spawned process with the pid attribute, so

    spawnedBash = pexpect.spawn('bash')
    print(spawnedBash.pid)
    

    should print your spawned process's pid