简体   繁体   English

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

[英]Replacing bash script with python - Checking if process is running?

This is what I have currently (checks a number of processes, this is just one) and it works magically: 这是我目前所拥有的(检查多个进程,这只是一个),并且神奇地起作用:

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

I'd like to move this to python, and most of the script I've been able to figure out and test, but I'm having issues with the block above. 我想将其移至python,大部分脚本都可以找到并测试,但是上面的代码块存在问题。 This is where I've gotten, but I'm not sure I can nest a try/except within an if/else statement, and would greatly appreciate some help: 这是我到达的地方,但是我不确定是否可以在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)

Where is my mistake within this??? 我的错误在哪里??? Many thanks in advance!! 提前谢谢了!!

You can nest a try/except inside an if/else, but you will need to be careful of indentation, as that is how Python indicates blocks of code. 您可以将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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM