Замена скрипта bash на python - Проверка, запущен ли процесс?

1

Это то, что я имею в настоящее время (проверяет несколько процессов, это всего лишь один), и он работает магически:

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 not expected on this host due to the role of $role."
fi

Я бы хотел переместить это на python, и большую часть скрипта я смог выяснить и проверить, но у меня проблемы с блоком выше. Это то, что я получил, но я не уверен, что могу вложить попытку/исключая внутри оператора if/else и очень ценю некоторую помощь:

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 not expected on this host due to the role of $role.")
print();print();time.sleep(0.5)

Где моя ошибка в этом? Спасибо заранее!!

Теги:

1 ответ

0
Лучший ответ

Вы можете вставить 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 not expected on this host due to the role of $role.")
print();print();time.sleep(0.5)

Ещё вопросы

Сообщество Overcoder
Наверх
Меню