无法在启动期间使os.getenv('HOSTNAME')工作

时间:2014-02-24 16:55:23

标签: python

我有在Linux VM启动期间运行的python脚本:

我已将其添加到chkconfig 345

脚本应该检查主机名,如果它是localhost.localdom那么它应该退出

#!/usr/bin/python
import subprocess,platform,os,sys,logging,shlex

system_name = os.getenv('HOSTNAME')

if system_name == 'localhost.localdom':
    logging.info('Please correct host name for proxies, it is showing localhost')
    sys.exit()"

如果我手动运行它可以正常工作。但在启动过程中,即使主机名是localhost.localdom。它没有退出。

所以在启动过程中看起来像

os.getenv('HOSTNAME')

没有返回我在条件中设置的localshot.localdom。

请帮助您在重启期间使其正常工作。

谢谢, Jitendra Singh

1 个答案:

答案 0 :(得分:1)

使用Isedev评论中的信息发布答案......

您可以尝试通过以下方式获取主机名:

import os
system_name = os.popen('/bin/hostname')

if system_name.read().rstrip() == 'localhost.localdom':
    logging.info('Please correct host name for proxies, it is showing localhost')
    sys.exit()