用python替换bash脚本-检查进程是否正在运行? - python

这是我目前拥有的(检查多个进程,这只是一个),并且神奇地起作用:

if role=Agent
then 
    echo "Validations for the service $servicename..."
        livefilterd=$(pgrep -f backgroundd)
            if [ -n "$livefilterd" ]
                    then
                            printf "The $servicename service is running as: \n$livefilterd"
                                let statusfilterd=1
                    else
                            echo -ne '!!!The $servicename process is NOT running!!!\r';sleep 0.5;echo -ne '   The $servicename process is NOT running   \r';sleep 0.5;echo -ne '!!!The $servicename process is NOT running!!!\r';sleep 0.5;echo -ne '   The $servicename process is NOT running   \r';sleep 0.5;
                                let "badkids=badkids+1"
                                let statusfilterd=0
            fi
else
    print "Validation for $servicename is being skipped as it's not expected on this host due to the role of $role."
fi

我想将其移至python,大部分脚本都可以找到并测试,但是上面的代码块存在问题。这是我到达的地方,但是我不确定是否可以在if / else语句中嵌套try / except,并且会非常感谢您的帮助:

servicename=backgroundd
if role == 'Agent': 
    print ("Validations for the service " + servicename + "...")
try:
    get_pid("backgroundd")    
    print ("YAY")
    print ("The " + servicename + " service is running as:" + servicename)
    statusfilterd = 1
except Exception:
    cprint("\n!!!!The " + servicename + " process is NOT running!!!!", 'red', attrs=['blink'])
    badkids = badkids+1
    statusfilterd = 0
else
    print ("Validation for $servicename is being skipped as it's not expected on this host due to the role of $role.")
print();print();time.sleep(0.5)

我的错误在哪里???提前谢谢了!!

参考方案

您可以将try / except嵌套在if / else中,但是您需要注意缩进,因为这就是Python指示代码块的方式。

servicename=backgroundd
if role == 'Agent': 
    print ("Validations for the service " + servicename + "...")
    try:
        get_pid("backgroundd")    
        print ("YAY")
        print ("The " + servicename + " service is running as:" + servicename)
        statusfilterd = 1
    except Exception:
        cprint("\n!!!!The " + servicename + " process is NOT running!!!!", 'red', attrs=['blink'])
        badkids = badkids+1
        statusfilterd = 0
else:
    print ("Validation for $servicename is being skipped as it's not expected on this host due to the role of $role.")
print();print();time.sleep(0.5)

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

无法注释掉涉及多行字符串的代码 - python

基本上,我很好奇这为什么会引发语法错误,以及如何用Python的方式来“注释掉”我未使用的代码部分,例如在调试会话期间。''' def foo(): '''does nothing''' ''' 参考方案 您可以使用三重双引号注释掉三重单引…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

您如何在列表内部调用一个字符串位置? - python

我一直在做迷宫游戏。我首先决定制作一个迷你教程。游戏开发才刚刚开始,现在我正在尝试使其向上发展。我正在尝试更改PlayerAre变量,但是它不起作用。我试过放在列表内和列表外。maze = ["o","*","*","*","*","*",…

Python-crontab模块 - python

我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…