简体   繁体   English

启动python脚本的Bash脚本不会自行停止

[英]Bash script that starts python script doesn't stop itself

I'm unsure if this actually is the problem, but let me explain: I have a python script that gets started by a bash script. 我不确定这实际上是不是问题,但让我解释一下:我有一个由bash脚本启动的python脚本。 The bash script's job is done then, but when I grep the ps aux the call is still present. 然后bash脚本的工作完成,但是当我greps ps aux时,调用仍然存在。

#!/bin/bash
export http_proxy='1.2.3.4:1234'
python -u /home/user/folder/myscript.py -some Parameters >> /folder/Logfile_stout.log 2>&1 

If I grep for ps aux | grep python 如果我greps for ps aux | grep python ps aux | grep python I get the python -u /home/user/folder/myscript.py -some Parameters as a result. ps aux | grep python我得到了python -u /home/user/folder/myscript.py -some Parameters作为结果。 According to the logfile the python script closed properly. 根据日志文件,python脚本正确关闭。 (Code to end the script is within the script itself.) The script gets started every hour and I still see all the calls from the hours before. (结束脚本的代码在脚本本身内。)脚本每小时启动一次,我仍然可以看到前几个小时的所有调用。

Thanks in advance for your help, tips or advice! 在此先感谢您的帮助,提示或建议!

The parent bash script will remain as long as the child (python script) is running. 只要子(python脚本)正在运行,父bash脚本将保留。 If you start the python script running in background (add & at end of python line) then the parent will exit. 如果你启动在后台运行的python脚本(在python行的末尾添加&),那么父将退出。

#!/bin/bash
export http_proxy='1.2.3.4:1234'
python -u /home/user/folder/myscript.py -some Parameters >> /folder/Logfile_stout.log 2>&1 &

If you examine the process list (eg 'ps -elf'). 如果检查进程列表(例如'ps -elf')。 It will show the child (if still running). 它将显示孩子(如果仍在运行)。 The child PPID (parent PID) will be 1(root PID) instead of the parent PID because the parent doesn't exist any more. 子PPID(父PID)将为1(根PID)而不是父PID,因为父级不再存在。

It could eventually be a problem if your python script never exits. 如果你的python脚本永远不会退出,它最终可能会成为一个问题。 You could make the parent script wait and kill the child, eg wait 30 secs and kill child if it is still present: 您可以让父脚本等待并杀死孩子,例如等待30秒并杀死孩子,如果它仍然存在:

#!/bin/bash
export http_proxy='1.2.3.4:1234'
python -u /home/user/folder/myscript.py -some Parameters >> /folder/Logfile_stout.log 2>&1 &
sleep 30
jobs
kill %1
kill -9 %1
jobs

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

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