Python在运行启动代理重新启动后没有运行Shell命令
问题描述:
因此,我通过启动代理在macOS 10.13上启动Python(2.7)脚本。脚本运行,并在执行过程中触发计算机的重新启动。当计算机重新启动并登录时,启动代理再次运行该脚本。该脚本读取日志,然后执行切换:从中断的地方继续。Python在运行启动代理重新启动后没有运行Shell命令
问题是,重新启动后,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,
路径/到/文本/工具doesn't run, and when I call
重新启动()`,它永远不会打开应用程序。 - 没有错误,产生至少我能找到
我在做什么错?这与工具路径有关吗?
答
更新:
事实证明,解决办法是在脚本的开头添加约20秒的延迟。看起来它试图在Window Server完成加载之前运行有问题的命令,并且将所有内容都吓坏了。不是一个特别优雅的解决方案,但它适用于我在这个项目中需要的东西。