Edit Written in C it works like a champ. I began noticing that the python would fail unpredictably. The bash solution worked too but I wanted to be able to easily check if it was running so I wrote the C version so I can see it when I do a ps -e.
In an attempt to address this issue and the real problem (VLC player continues to inhibit power management daemon after playback has ended) I wrote the following script. It works when I run it from IDLE but not from terminal. My ultimate goal is to have this script run as a startup application. I'm using Ubuntu 12.04 and Python 2.7.3. What do I need to change to achieve the desired result? Please forgive my amateur python skills. Thanks in advance
EDIT: The script runs in terminal since I took out t.daemon = True (thanks Max Noel) however the script still doesn't run as a startup application using the command python /path/to/script/vlc_watchdog.py and also I noticed that when it is run from terminal, it only works so long as the terminal window remains open. I want this process to go run off and be happy and free and independent of it's parent, even after the parent is closed/killed. How do I make that happen?
What almost worked: sudo apt-get install python-daemon ; add import daemon to the script ; move import time to the top and add time.sleep(30) immediately after ; do away with all the thread stuff and in it's place put with daemon.DaemonContext(): run() ; in the Startup Applications window of Ubuntu 12.04 add an entry that executes the command python /path/to/script/vlc_watchdog_daemon.py &. That & at the end is important. My best guess (and I'm a n00b at both Ubuntu and Python) is that there was something(s) that wasn't fully loaded and this not-quite-fully-loaded condition was causing the script to behave abnormally. That is why I added the sleep immediately following the import time and added the & at the end of the Startup Applications command. I could have perhaps gotten away with doing less but after the headache I'm at the point where "IT WORKS DON'T TOUCH IT!!!" It may be possible to remove "fixes" and still get it to work. You the reader are welcome to try. I'm good. Final version of script at end of question.
Original Version Didn't Quite Work as a Startup Script but works from IDLE and Terminal:
#vlc_watchdog.py version 0.1 import dbus import os import time import subprocess from subprocess import Popen, PIPE from threading import Thread def vlc_killer(): bus = dbus.SessionBus() vlc_media_player_obj = bus.get_object("org.mpris.MediaPlayer2.vlc", "/org/mpris/MediaPlayer2") props_iface = dbus.Interface(vlc_media_player_obj, 'org.freedesktop.DBus.Properties') pb_stat = props_iface.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus') if pb_stat == 'Stopped': os.system("kill -9 $(pidof vlc)") def vlc_is_running(): ps = subprocess.Popen(['ps', '-e'], stdout = PIPE) out, err = ps.communicate() for line in out.splitlines(): if 'vlc' in line: return True return False def run(): while True: if vlc_is_running(): vlc_killer() else: time.sleep(30) t = Thread(target=run) #t.daemon = True <-- this is what broke it. Thanks Max Noel t.start() Final Version Works as Intended:
#!/usr/bin/env python import time time.sleep(30) import dbus import os import subprocess from subprocess import Popen, PIPE import daemon import setproctitle sleeptime = 15 def vlc_killer(): bus = dbus.SessionBus() vlc_media_player_obj = bus.get_object("org.mpris.MediaPlayer2.vlc", "/org/mpris/MediaPlayer2") props_iface = dbus.Interface(vlc_media_player_obj, 'org.freedesktop.DBus.Properties') pb_stat = props_iface.Get('org.mpris.MediaPlayer2.Player', 'PlaybackStatus') if pb_stat == 'Stopped': os.system("kill -9 $(pidof vlc)") else: time.sleep(sleeptime) def vlc_is_running(): ps = subprocess.Popen(['ps', '-e'], stdout = PIPE) out, err = ps.communicate() for line in out.splitlines(): if 'vlc' in line: return True return False def run(): setproctitle.setproctitle('vlc-watchdog') while True: if vlc_is_running(): vlc_killer() else: time.sleep(sleeptime) with daemon.DaemonContext(): run() Executed at startup by adding python /path/to/script/vlc_watchdog_daemon.py & to the Startup Applications window in Ubuntu 12.04. See Adding Startup Applications for detailed instructions. I'm running Python 2.7.3.
user@notebook:~$ python ~/python/vlc-watchdog.pyuser@notebook:~$