【问题标题】:python script to call itself with xvfbpython脚本用xvfb调用自己
【发布时间】:2016-06-01 18:22:48
【问题描述】:

我编写了一个使用ete3 package 生成系统发育树的脚本,该脚本在无头服务器上运行,因此它必须使用xvfb-run 启动(根据here)。

我已经设置了脚本来检查(通过对ps 的系统调用)是否使用 xvfb 调用它。如果在没有 xvfb-run 的情况下启动 python 脚本(例如python script.py...),我是否有一种直接的方法可以从原始脚本调用中终止进程并正确重新运行它(例如xvfb-run python script.py...)?

我尝试通过os.system() 调用ps 来破解某些东西,但我运气不佳。有人有什么建议吗?

【问题讨论】:

    标签: python xvfb


    【解决方案1】:

    我能够将一些东西放在一起,只需将函数 check_xvfb() 添加到脚本的开头即可。

    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!'
    

    【讨论】:

      猜你喜欢
      • 2016-12-17
      • 2011-10-01
      • 1970-01-01
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多