我有一个bash脚本,该脚本可以永远运行,并以不同的间隔打印一些信息。为简单起见;假设这是我的bash脚本;我们称之为mybashscript.sh
#!/bin/bash
while :
do
echo "The loop has printed"
sleep 5
done
这仅仅是出于简单的原因;我的实际脚本不是那么简单。 (我有大约30种非分支,非subshell启动的bash脚本。)
我想编写一个将运行的python程序;
[python]
)并打印出来。我有以下程序可以做到这一点;让我们称之为mywrapper.py
(我在这里使用命令mpc next
;因为如果被调用,这对我来说很容易实现;我戴着耳机)
#!/bin/env python
import os
import re
import sys
import threading
import subprocess
def response():
""" Just one function to check if functions work """
subprocess.call(['mpc', 'next'],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
def listen(inp):
""" Respond to stdin """
for line in inp:
response()
if __name__ == '__main__':
# Test the response function
response()
# Start the infinite loop
SCRIPT = subprocess.Popen('/tmp/mybashscript.sh',
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)
# Start the listener
LISTEN = threading.Thread(target=listen,
args=(sys.stdin))
LISTEN.start()
# Respond to lines in the script
while True:
LINE = SCRIPT.stdout.readline()
# If the script terminated; get out of loop to terminate program
if not LINE:
break
print('[python]' + str(LINE))
# Need this to stop buffering
sys.stdout.flush()
但是; listen
没有执行任何操作。响应功能工作正常;在第一次调用时。如何更改我的python脚本以响应输入?
也;需要两个键盘中断才能停止该程序。如何判断SIGTERM是否也会杀死所有子进程?
为避免XY问题;这正是我想要做的;
music
的 dash 脚本,它打印当前正在播放的歌曲的名称,并且仅在歌曲更改时才打印新行。这条路;如果我不播放任何歌曲,我不必轮询信息,也不必不断更新文本。sysinfo-i3bar
的python包装器;基本上将字符串的输出格式化为json格式;因为我可以控制颜色和东西。 (我之所以不对破折号脚本进行完整的json解析,是因为我想重用原始脚本;但是在其他程序中,例如polybar;使用一个信息脚本,然后使用多个包装来处理多个场合因此很有帮助)到目前为止,一切都很好。mpc toggle
。我目前不知道该怎么做。sysinfo-polybar
包装器中所示。)最终;我想打包这些脚本/包装并将其发布给社区。感谢您的帮助。