fdfv fdfv - 1 month ago 5
Python Question

How to invoke a Python script automatically when VLC media process starts everytime on Ubuntu?

I'm making a Python script that will save the song path whenever VLC media player runs on a Ubuntu System. The script is ready but if I run this script at the startup, it continuously uses cpu share in the loop. I want to invoke the script automatically whenever the user starts vlc media player. How can I do it? Can it be possible with any shell scripting?

Answer
import os,time

processname = 'enter the VLC process name'
tmp = os.popen("ps -Af").read()
proccount = tmp.count(processname)

while proccount > 0:
    print(proccount, ' processes running of ', processname, 'type')
    runsongpathfunction()
    time.sleep(1)

So what this does is it finds the process from the process name then when VLC is closed the script closes and it stops doing whatever it was doing while VLC was running because the while loop is no longer True. I added a time.sleep(1) to save resources its not really needed but it saves CPU.