簡體   English   中英

如何使用 Python 和 pexpect 在多個會話中運行 linux 可執行文件

[英]How to run a linux executable in multiple sessions using Python and pexpect

我正在運行 Ubuntu 18 服務器,並且我有一個接收某個觸發器的 Python 腳本。 我想在滿足觸發條件時運行一個可執行文件(帶有一組基於觸發器的參數)。 由於最大執行時間為 20 分鍾並且正在發生重疊,因此可以進行兩個或多個並行執行。

讓我們假設 Python 腳本正在運行,並且在時間 t1 觸發觸發器。 我想像這樣執行 smth:./executable param1 param2。 可執行文件可以自行停止,或者如果我以某種方式發送 ctrl+c 然后“停止”。 無論如何,我希望它在最多 20 分鍾后停止。

在時間 t2 觸發器再次觸發。 我想執行 smth 之類的: ./executable param3 param4 而前一次執行可能尚未完成。 我再次希望這在最多 20 分鍾后停止。

在時間 t3 可能會觸發另一個觸發器。 我希望你知道流程是如何進行的。

pexpect 是我需要的嗎? 我怎么能解決這個問題?

非常感謝,如果描述不夠清楚,我很抱歉。

您可以隨時將孩子放在一個列表中,並根據需要與每個孩子進行互動。 這是一個示例(我使用了 10 秒而不是 20 分鍾):

#!/usr/bin/python3
import pexpect
from datetime import datetime, timedelta
import time

print("Pexpect Children List Example:")
print("Spawning the child processes and adding them to a list...")
children = []
tod = datetime.utcnow()
start_time = datetime.utcnow()
while tod <= start_time + timedelta(seconds=10):
    child = pexpect.spawn("/bin/bash")
    # Always expect after a spawn or send
    child.expect_exact("(venv) ")
    children.append(child)
    time.sleep(2)
    tod = datetime.utcnow()

print("You spawned {0} children.".format(len(children)))

command_output = pexpect.run("/bin/bash -c 'ps aux | grep /bin/bash'")
print(command_output.decode("utf-8"))

print("Interacting with each child...")
for index, c in enumerate(children):
    command = "echo 'This is child {0}'".format(index)
    c.sendline(command)
    # Virtual environment. Expect (venv) instead of ]$
    c.expect_exact("(venv)")
    print(c.before.decode("utf-8").strip())
    c.sendline("ps")
    c.expect_exact("(venv)")
    print(c.before.decode("utf-8"))

print("Closing children...")

for index, c in enumerate(children):
    c.close()
    print("Child[{0}] closed.".format(index))

print("Script complete. Have a nice day.")

輸出

Pexpect Children List Example:
Spawning the child processes and adding them to a list...
You spawned 5 children.
me  17004  0.3  0.1 116800  3176 pts/1    Ss+  19:47   0:00 /bin/bash
me  17039  0.2  0.1 116800  3180 pts/2    Ss+  19:47   0:00 /bin/bash
me  17074  0.3  0.1 116800  3172 pts/3    Ss+  19:47   0:00 /bin/bash
me  17118  0.7  0.1 116800  3180 pts/4    Ss+  19:48   0:00 /bin/bash
me  17152  1.0  0.1 116800  3172 pts/5    Ss+  19:48   0:00 /bin/bash
me  17187  0.0  0.0 113280  1200 pts/6    Ss+  19:48   0:00 /bin/bash -c ps aux | grep /bin/bash
me  17189  0.0  0.0 112812   948 pts/6    S+   19:48   0:00 grep /bin/bash

Interacting with each child...
echo 'This is child 0'
This is child 0
 ps
  PID TTY          TIME CMD
17004 pts/1    00:00:00 bash
17190 pts/1    00:00:00 ps

echo 'This is child 1'
This is child 1
 ps
  PID TTY          TIME CMD
17039 pts/2    00:00:00 bash
17191 pts/2    00:00:00 ps

echo 'This is child 2'
This is child 2
 ps
  PID TTY          TIME CMD
17074 pts/3    00:00:00 bash
17192 pts/3    00:00:00 ps

echo 'This is child 3'
This is child 3
 ps
  PID TTY          TIME CMD
17118 pts/4    00:00:00 bash
17193 pts/4    00:00:00 ps

echo 'This is child 4'
This is child 4
 ps
  PID TTY          TIME CMD
17152 pts/5    00:00:00 bash
17194 pts/5    00:00:00 ps

Closing children...
Child[0] closed.
Child[1] closed.
Child[2] closed.
Child[3] closed.
Child[4] closed.
Script complete. Have a nice day.

您可以看到每個子 PID 都與父列表中的一個 PID 匹配。

順便說一句,我會考慮使用cron以定時間隔運行腳本來輪詢觸發器,而不是連續運行 Python 腳本。 但是,這可能對您不起作用。

祝你的代碼好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM