所以,我在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,
路径/到/文本/工具doesn't run, and when I call
重新启动()`,它永远不会打开应用。我做错了什么?它与工具路径有关吗?
答案 0 :(得分:0)
更新
事实证明,解决方案是在脚本开始时添加约20秒的延迟。似乎它试图在Window Server完成加载之前运行有问题的命令,这吓坏了一切。不是一个特别优雅的解决方案,但它适用于我在这个项目中需要的东西。