简体   繁体   English

如何阻止子进程终止我的脚本?

[英]How to stop subprocess from terminating my script?

Whenever I am trying to run a shell script using subprocess.call() or os.system() , the script runs, but the Python script also terminates and everything written after the call never executes.每当我尝试使用 subprocess.call subprocess.call()os.system()运行 shell 脚本时,该脚本都会运行,但 Python 脚本也会终止,调用后编写的所有内容都不会执行。 I have tried importing this call from a library and running or executing it from a separate python script using execfile() but same thing happened there too.我尝试从库中导入此调用并使用execfile()从单独的 python 脚本运行或执行它,但同样的事情也发生在那里。 Is there something wrong with my system, or is this how it is supposed to be?我的系统有问题吗,或者这就是应该的样子? If the latter, then how shall I stop this and keep my Python script running after making this subprocess/system call?如果是后者,那么在进行此子进程/系统调用后,我该如何停止并保持我的 Python 脚本运行?

shushens@P600:~/Desktop$ python
Python 2.7.2+ (default, Oct  4 2011, 20:03:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(["sh","test.sh"])
shushens@P600:~/Desktop$

This is the shell script:这是 shell 脚本:

export <SOMEPATHNAME>=/some/path/here
exec $SHELL -i

I think it is the exec $SHELL -i that is causing all programs running on that particular shell to terminate.我认为是exec $SHELL -i导致在该特定 shell 上运行的所有程序终止。 But I do not know what other alternative I have.但我不知道我还有什么其他选择。 The export does not work if I do not use it.如果我不使用它, export不起作用。 Currently, the path I want to export is exporting, but the Python process gets terminated along with it.目前,我要导出的路径正在导出,但 Python 进程也随之终止。

Thanks in advance!提前致谢!

The shell isn't killing your script, it's doing exactly what you ask it to do, start a new interactive bash session. Notice that after the call to subprocess if I do a ps, python is still running. shell 并没有杀死你的脚本,它正在做你要求它做的事情,启动一个新的交互式 bash session。请注意,如果我执行 ps 调用子进程后,python 仍在运行。 Further more if you exit the bash session it takes you back into the python interpreter/script.此外,如果您退出 bash session,它会将您带回 python 解释器/脚本。

bago@bago-laptop:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.call(['sh', 'test.sh'])
bago@bago-laptop:~$ ps
  PID TTY          TIME CMD
 1412 pts/3    00:00:00 bash
 1485 pts/3    00:00:00 python
 1486 pts/3    00:00:00 bash
 1509 pts/3    00:00:00 ps
bago@bago-laptop:~$ exit
exit
0
>>> print "im back in python"
"im back in python"
>>> exit()

Export shouldn't need you start a new bash session to work.导出不需要你启动一个新的 bash session 来工作。 I'm not sure why you're using export but have you considered os.environ['SOMEPATHNAME'] = "/some/path/here" .我不确定您为什么使用 export 但您是否考虑过os.environ['SOMEPATHNAME'] = "/some/path/here" This will set the environment variable in your python script before you use subprocess.call .这将在您使用subprocess.call之前在您的 python 脚本中设置环境变量。

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

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