简体   繁体   English

停止从Mac终端运行python shell脚本

[英]Stop running python shell script from Mac terminal

I started my shell script using something like the following: 我使用以下内容启动了shell脚本:

sh python3_start.sh

How do I stop this completely? 如何完全停止呢? If the process is already running and I do a cntrl + c it seems like it stops it but I don't know if it's not entirely killed in the background. 如果该进程已经在运行,并且我执行了cntrl + c则似乎将其停止,但是我不知道它是否在后台没有被完全杀死。

Run the script, while it is running type the following into a different terminal session 在运行脚本的同时运行它,在不同的终端会话中输入以下内容

top

If you see the script running in there then it is, if not the script has stopped. 如果您看到脚本在其中运行,那么脚本就已经停止了。 You might want to put some temporary sleep timers in your script so you can be sure. 您可能希望在脚本中放置一些临时睡眠计时器,这样可以确定。 To do this add this somewhere within your python code. 为此,请将其添加到您的python代码中的某个位置。 Make sure you are also importing time as well. 确保您也导入了时间。

time.sleep(30)

If you need to kill the script from terminal you can do this as well. 如果您需要从终端杀死脚本,也可以这样做。 To do this run the following command in a different terminal session. 为此,请在其他终端会话中运行以下命令。

kill -9 "pid next to script in top" 

Run the following command to list all processes and extract the ones that contain "python3_start.sh": 运行以下命令以列出所有进程并提取包含“ python3_start.sh”的进程:

ps ax | grep -i python3_start.sh

then you can use the "pid" ("pid" is the number in the beginning of each line) and run: 那么您可以使用“ pid”(“ pid”是每行开头的数字)并运行:

kill "pid"

if the first command only produces something like this: 如果第一个命令仅产生以下内容:

28952 s000  S+     0:00.01 grep -i python3_start.sh

then you don't have your script running in the background. 那么您就不必在后台运行脚本了。

Change "python3_start.sh" in the first command to any process you want to see is running and follow the same steps to kill it. 将第一个命令中的“ python3_start.sh”更改为要查看正在运行的任何进程,并按照相同的步骤将其杀死。

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

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