如何使用机器人框架检查unix进程是否正在运行

时间:2017-04-24 13:16:35

标签: robotframework

我正在尝试检查进程是否正在从unix系统中主动运行。它可以是一个进程或多个进程。以下是我正在尝试的示例。有人可以指导我如何实现这一目标吗?

*** Settings ***
Library    Process
Library           OperatingSystem

*** Test cases ***
Example
     ${output} =     Run process   /etc/init.d/bluetooth
     ${op}=     Is Process Running   ${output} 
     Should be equal     ${op}     True

[root@test ssh-scripts]# pybot test-process.robot 
==============================================================================
Test-Process                                                                  
==============================================================================
Example                                                               | FAIL |
Non-existing index or alias '<result object with rc 3>'.
------------------------------------------------------------------------------
Test-Process                                                          | FAIL |
1 critical test, 0 passed, 1 failed
1 test total, 0 passed, 1 failed
==============================================================================

2 个答案:

答案 0 :(得分:2)

  1. /etc/init.d/bluetooth是启动脚本,您不会检查启动脚本,而是检查由它启动的进程

  2. 关键字Run Process等待,直到流程终止。您可能不会等待终止,而是希望在后台运行该过程。

  3. 这对我有用:

    Test Processes
        ${handle}    Start Process    echo    foo    stdout=/tmp/bar
        ${output}    Is Process Running    handle=${handle}
        Should Not Be True    ${output}
        ${handle}    Start Process    yes    stdout=/dev/null
        ${output}    Is Process Running    handle=${handle}
        Should Be True    ${output}
        [Teardown]    Terminate All Processes
    

    即。 echo在执行第一次Is Process Running检查之前终止。但yes已启动,并在第二个Is Process Running时刻继续运行。

答案 1 :(得分:0)

以下脚本为我工作。在执行命令后,我已经放置了一个正则表达式模式来匹配字符串。这可以通过其他方式实现,但这符合我的目的。

*** Settings ***

Library                Process
Library                SSHLibrary
Suite Setup            Open Connection And Log In
Suite Teardown         Close All Connections
Library                OperatingSystem

*** Variables ***

${HOST}                16.10.90.24
${USERNAME}            root
${PASSWORD}            admin123
${postgres_cmd}         /etc/init.d/DB_Platform_TaskManager  status
${PATTERN}             (?m).*is\ running


*** Test Cases ***
Check whether postgres is running 
   Open Connection And Log In
   Start Command     ${postgres_cmd}
   ${rc}=    Read Command Output   return_stdout=True   return_rc=False
   Log    ${rc}
   Should Match Regexp    ${rc}     ${PATTERN}

*** Keywords ***
Open Connection And Log In
   Open Connection    ${HOST}
   Login    ${USERNAME}    ${PASSWORD}