我想在我的python脚本中退出logstash,然后在cmd中单击ctrl-c将其关闭。我试图使用其中的一个函数,但它不起作用。任何人都有一个想法,请告诉我怎么做。
import os
import subprocess
import signal
@main.route('/data')
def data():
...
target_dir = os.path.join("C:\elastic_stack\logstash-7.6.2")
# small check
if os.path.isdir(target_dir):
os.chdir(target_dir)
else:
print(" pathname does not refer to an existing directory")
# current working directory
print(os.getcwd())
# start logstash directly os.system will return the return code of the command if it's 0 means OK
os.system(".\\bin\\logstash -f C:\\Users\\Asus\\Dropbox\\PFE_part2\\New_flask_app\\logstash.conf")
# if you need the output after you started logstash it will work ONLY in Python3
process = subprocess.Popen([".\\bin\\logstash", "-f", "C:\\Users\\Asus\\Dropbox\\PFE_part2\\New_flask_app\\logstash.conf"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = process.stdout
errors = process.stderrt
#After logstash finished the work above, it shut down
os.kill(process.pid, signal.SIGTERM) #that doesn't work
process.sendcontrol('c') #or either this
process.close()
return render_template('data.html')发布于 2020-11-26 19:51:49
在C#中,我使用NuGet包Renci,打开CreateShellStream并发送命令shellStream.Write("\u0003");,然后发送sudo命令并执行任务。\u0003是针对Ctrl + C的。也许你可以试着用Python发送它。
https://stackoverflow.com/questions/61689941
复制相似问题