2017-11-17 21 views
1

그래서 MacOS 10.13에서 실행 에이전트를 통해 Python (2.7) 스크립트를 시작합니다. 스크립트가 실행되고 실행 중에 컴퓨터가 다시 시작됩니다. 컴퓨터가 다시 켜지고 로그인하면 시작 에이전트가 스크립트를 다시 실행합니다. 스크립트는 로그를 읽고 스위치 : 대소 문자를 사용하여 중단 한 부분부터 다시 시작합니다.Python이 실행 에이전트를 사용하여 재부팅 한 후 셸 명령을 실행하지 않습니다.

문제는 재시작 후 python 스크립트가 일부 셸 명령을 실행할 수 없다는 것입니다. 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 restarts()`, 앱을 열지 않습니다.
  • 오류는 적어도 내가 뭘 잘못

를 찾을 수 생산되지 않습니다? 도구 경로와 관련이 있습니까?

답변

0

는 업데이트 :

그것이 나오는 것에 따라,이 솔루션은 스크립트의 시작에서 20 ~ 초 지연을 추가했다. Window Server가로드를 완료하기 전에 문제의 명령을 실행하려고 시도하고있는 것처럼 보였습니다. 특히 우아한 솔루션은 아니지만이 프로젝트에서 필요한 기능을 제공합니다.