【问题标题】:Python Not Running Shell Commands After Reboot Using Launch Agent使用启动代理重新启动后 Python 不运行 Shell 命令
【发布时间】:2018-05-01 19:17:24
【问题描述】:

所以,我正在 macOS 10.13 上通过启动代理启动 Python (2.7) 脚本。该脚本运行,并在执行期间触发计算机重新启动。当计算机重新打开并登录时,启动代理会再次运行该脚本。该脚本读取日志,并执行 switch:case 以从中断处继续。

问题是,重启后python脚本无法执行一些shell命令。 ls -l 工作正常。但是,当我尝试运行不在 /bin 中的东西时,它似乎只是......跳过它。没有错误,它根本不这样做。

这是相关代码。我已经删除了大部分细节以及开关控制,因为我已经验证它们可以独立工作。

#!/usr/bin/python

import logging
import os
import subprocess
import time

#globals
path_to_plist = 'User/Me/Library/Preferences/PathTo.plist'


def spot_check():
    #determine where we are in the test, verified it works
    return loop_number

def func_that_checks_stuff():
    results = subprocess.check_output(['System/Library/Path/To/tool','-toolarg'])
    ###process results
    logging.info(results)

def restarts(power_on, power off):
    #sets plist key values for the restart app
    subprocess.Popen('defaults write {} plist_key1 {}'.format(path_to_plist, power_on), shell=True
    subprocess.Popen('defaults write {} plist_key2 {}'.format(path_to_plist, power_off), shell=True

    #run the restart app, which is a GUI application in /Applications
    logging.info('Starting restart app')
    subprocess.Popen('open -a RestartApp.app', shell=True)
    time.sleep(power_on + 5)

def main():
    ###setup and config stuff, verified its working

    #switch control stuff, verified its working
    loop = spot_check()

    if loop == 0:
        #tool that shows text on the screen
        subprocess.Popen('User/Me/Library/Scripts/Path/To/Text/tool -a -args', shell=True)
        logging.info('I just ran that tool')
        subprocess.check_output('ls -l', shell=True)
        restarts(10, 0)
    if loop == 1:
        func_that_checks_stuff()
        subprocess.Popen('User/Me/Library/Scripts/Path/To/Text/tool -a args', shell=True)
        logging.info('Hey I ran that tool again, see?')
        restarts(10, 0)
    else:
        func_that_checks_stuff()
        subprocess.Popen('User/Me/Library/Scripts/Path/To/Text/tool -a args', shell=True)

    print 'You finished!'

if __name__ == '__main__':
    main()

所以,如果我使用启动代理启动它,它将很好地运行每个序列。

  • 在第一个循环中(重新启动之前),一切正常。所有日志记录,所有工具,一切。
  • 重新启动后,所有的日志记录工作,所以我知道它遵循开关控制。 func_that_checks_stuff() 工作,并正确记录它的输出。 ls -l' call shows me exactly what I should see. But,Path/To/Text/tooldoesn't run, and when I callrestarts()`,它永远不会打开应用程序。
  • 没有产生错误,至少我能找到

我做错了什么?它与工具路径有关吗?

【问题讨论】:

    标签: bash python-2.7 subprocess launchd


    【解决方案1】:

    更新:

    事实证明,解决方案是在脚本开始时添加约 20 秒的延迟。似乎它试图在 Window Server 完成加载之前运行有问题的命令,这把一切都吓坏了。不是一个特别优雅的解决方案,但它可以满足我在这个项目中的需要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      相关资源
      最近更新 更多