简体   繁体   中英

Start a process with python and get the PID (Linux)

I'm working with Iot on the Intel Galileo with a Yocto image, I have it that a python script will execute 'aplay audio.wav', But I want it to also get the PID of that aplay proccess in case the program will have to stop it. Sorry for being very short and brief.

The pid attribute of the subprocess.Popen object contains its PID, but if you need to terminate the subprocess then you should just use the terminate() method.

You should consider using pyao or pygst/gst-python instead though, if you need finer control over audio.

With Python use:

import os

my_pid = os.getpid()
f_pid = open('/var/run/app.pid', 'w')
f_pid.write(my_pid)
f_pid.close()

But if you are using the play, you're doing syscall() for Python? You can do directly by Shell Script and get the PID easily with:

echo $! > /var/run/$0.pid

You can read the file.pid and send a kill for the PID.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM