简体   繁体   English

如何通过 Ansible 停止正在运行的 python 脚本?

[英]How to stop a running python script by Ansible?

In the node1 host, there is a Python script py_script.py :node1主机中,有一个 Python 脚本py_script.py

import time

while True:

    print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

    time.sleep(1)

then in my ansible host, I can use the ansible to start the script:然后在我的ansible主机中,我可以使用 ansible 来启动脚本:

ansible node1 -m shell -a 'python py_script.py &'

then it will start running.然后它将开始运行。

but how can I stop it?但我怎么能阻止呢?

It seems like you should have systemd manage your script or have some other kind of service.似乎您应该让systemd管理您的脚本或提供其他类型的服务。

If you do not want that, you can use pkill to kill this script:如果你不想这样,你可以使用pkillpkill这个脚本:
pkill -f "python py_script.py"
This will kill all processes with the command python py_script.py .这将使用命令python py_script.py所有进程。

You can do ansible node1 -m shell -a 'pkill -f "python py_script.py"'你可以做ansible node1 -m shell -a 'pkill -f "python py_script.py"'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM