Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a program using python?

Tags:

python

I am trying to use python to close VLC while it is recording audio. Currently I am using:

os.kill(pid,pid)

This works but is closing VLC abruptly and not allowing the recording file to close properly, thus corrupting it. If I manually close the VLC GUI instance than the recording file will not be corrupted.

So basically I am looking for a python command to close an application that emulates the 'Close' button on the application's GUI.

Or, perhaps there is another way. Such as closing the .wav file where the recording is being written, before killing the VLC process? I also put some work into this, but didn't get any results.

Thanks for the help!

like image 406
user2564290 Avatar asked Aug 28 '26 20:08

user2564290


1 Answers

You're using the function wrong, you'll likely want something like this:

from signal import SIGTERM
os.kill(pid,SIGTERM)

The second parameter specifies the interrupt. Also often used is SIGKILL which is a hard kill, likely same as you had before. You can find more about the linux signals here. In windows your options are more limited, see the python docs for available signals.

By providing pid also for the second parameter you probably set a quite heavy kill signal, that terminated the application immediately, without closing files.

like image 129
KillianDS Avatar answered Aug 31 '26 19:08

KillianDS