简体   繁体   English

为什么此Python脚本不能在Ubuntu 12.04中作为启动应用程序运行?

[英]Why won't this Python script run as a startup application in Ubuntu 12.04?

I've written this watchdog script to monitor VLC player and kill it when playback has stopped because VLC continues to inhibit the power management daemon after playback. 我编写了这个看门狗脚本来监视VLC播放器,并在播放停止时将其杀死,因为VLC在播放后继续禁止电源管理守护程序。 The script works. 该脚本有效。 I can run it from the command line or through IDLE and it kills VLC when playback stops. 我可以从命令行或通过IDLE运行它,并且在播放停止时它会杀死VLC。 I've added many variations of the command to start the script to my startup applications as described here but when I reboot, if it is running at all, it stops as soon as VLC starts. 我已经添加了许多命令的变体来启动脚本,如此处所述但是当我重新启动时,如果脚本完全在运行,则它将在VLC启动后立即停止。 Restarting it from a terminal cause it to stay running and do what it is supposed to do. 从终端重新启动它会使其保持运行并执行应做的事情。 I don't know if this is a problem with the script or something peculiar about Ubuntu Startup Applications (although I'm leaning towards Ubuntu). 我不知道这是脚本问题还是Ubuntu启动应用程序特有的问题(尽管我倾向于Ubuntu)。 Maybe something to do with permissions? 也许与权限有关? (Although I did chmod +x ) Should I be executing some other commands to make sure DBus is up before I launch the script? (尽管我做了chmod +x )我是否应该在启动脚本之前执行其他命令以确保DBus已启动? Part of me thinks that something isn't fully loaded when the script starts so I tried sleeping before launching using the *nix sleep command, the X-GNOME-Autostart-Delay , and time.sleep(n) in the python code. 我的一部分认为脚本启动时未完全加载某些内容,因此在使用* nix sleep命令, X-GNOME-Autostart-Delay和python代码中的time.sleep(n) X-GNOME-Autostart-Delay之前,我尝试了休眠。 The pythonic way seems to have the best chance of success. pythonic方式似乎有最大的成功机会。 The *nix ways seem to only make startup take longer and at the end of it I find that the process isn't even running. * nix方式似乎只会使启动花费更长的时间,最后,我发现该进程甚至没有运行。 I'm using the python-setproctitle module to name the process so I can quickly see if it is running with a ps -e from terminal. 我正在使用python-setproctitle模块命名该进程,以便可以快速从终端查看它是否以ps -e运行。 I'm out of ideas and about ready to just manually run the script whenever I reboot (although in principle I think that the machine should do it for me because I told it to). 我没有主意,只是准备在每次重新启动时手动运行脚本(尽管原则上我认为机器应该为我做这件事,因为我告诉过我)。 Some variations of Startup Application command lines that I've tried are: 我尝试过的“启动应用程序”命令行的一些变体是:

/path/to/script/vlc_watchdog.py
"/path/to/script/vlc_watchdog.py"
/path/to/script/vlc_watchdog.py &
"/path/to/script/vlc_watchdog.py &"
python /path/to/script/vlc_watchdog.py
python /path/to/script/vlc_watchdog.py &
"python /path/to/script/vlc_watchdog.py"
"python /path/to/script/vlc_watchdog.py &"
bash -c "/path/to/script/vlc_watchdog.py"
sleep 30 ; /path/to/script/vlc_watchdog.py
sleep 30 && /path/to/script/vlc_watchdog.py
etc...

Full script: 完整脚本:

#!/usr/bin/env python
import time
time.sleep(30)
import dbus
import os
import subprocess
from subprocess import Popen, PIPE
import daemon
import setproctitle

setproctitle.setproctitle('VLC-Watchdog')
sleeptime = 5

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():
    while True:
        if vlc_is_running():
            vlc_killer()
        else:
            time.sleep(sleeptime)

with daemon.DaemonContext():
    run()

In the shell script that starts your Python code (the one in the Ubuntu startup/initialization process), use something like: 在启动Python代码的shell脚本(Ubuntu启动/初始化过程中的脚本)中,使用类似以下内容的代码:

#!/bin/sh

set -x
exec > /tmp/errors.out 2>&1
/path/to/script/vlc_watchdog.py

Then after things go awry again (that is, after another reboot), inspect /tmp/errors.out to see the error messages related to whatever happened. 然后,在一切再次出现问题之后(也就是在重新启动后),检查/tmp/errors.out以查看与所发生的事情相关的错误消息。 There should be a Python traceback in there, or at least a shell error. 那里应该有Python追溯,或者至少是一个shell错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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