2016-06-01 13 views
0

anzurufen Ich habe ein Skript geschrieben, das einen phylogenischen Baum mit dem ete3 package generiert, das Skript läuft auf einem kopflosen Server, so dass es mit xvfb-run gestartet werden muss (per here).Python-Skript, um sich mit xvfb

Ich habe das Skript eingerichtet, um zu überprüfen (durch einen Systemanruf an ps), wenn es mit xvfb aufgerufen wurde. In dem Fall, in dem das Python-Skript ohne xvfb-run gestartet wird (z. B. python script.py...), gibt es eine direkte Möglichkeit für mich, den Prozess zu beenden und ihn korrekt (z. B. xvfb-run python script.py...) aus dem ursprünglichen Skriptaufruf auszuführen?

Ich habe versucht, etwas zusammen mit os.system() Anrufe zu ps zu hacken, aber ich habe nicht viel Glück. Hat jemand irgendwelche Vorschläge?

Antwort

1

Ich konnte etwas zusammenfügen, fügen Sie einfach die Funktion check_xvfb() an den Anfang Ihres Skripts.

def check_xvfb(): 
    """ 
    Use of the ete3 library from the command line requires an X11 server 
    which doesn't exist on this headless Ubuntu server. One way around this 
    is to use xvfb-run. This function checks that the script was properly 
    launched with xvfb-run; if not, it will relaunch it (with the same options) 
    and then terminate the previously called script 
                                      Parameters 
    ----------                                none 

    Returns 
    ------- 
    none 

    """ 

    # CHECK IF SCRIPT PROPERLY LAUNCHED 
    # see http://stackoverflow.com/a/6550543/1153897 for explanation of 'cat' 
    # grep -v ignores the ps -ef call since it'll match itself 
    comm = 'ps -ef | grep xvfb-run | grep %s | grep -v grep | cat' %os.path.splitext(os.path.basename(sys.argv[0]))[0] 
    output = subprocess.check_output(comm, shell=True) 

    if not len(output): # script not called properly 
     print 'script not called properly, retrying...' 
     comm_run = 'xvfb-run ' + ' '.join(sys.argv) 
     os.system(comm_run) # properly call script 
     sys.exit(0) 
    else: 
     print 'script called properly!' 
Verwandte Themen