2017-11-17 254 views
1

因此,我通過啓動代理在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重新啓動()`,它永遠不會打開應用程序。
  • 沒有錯誤,產生至少我能找到

我在做什麼錯?這與工具路徑有關嗎?

回答

0

更新:

事實證明,解決辦法是在腳本的開頭添加約20秒的延遲。看起來它試圖在Window Server完成加載之前運行有問題的命令,並且將所有內容都嚇壞了。不是一個特別優雅的解決方案,但它適用於我在這個項目中需要的東西。