Reputation: 6764
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. 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. 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?
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:
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. But I do not know what other alternative I have. The export
does not work if I do not use it. Currently, the path I want to export is exporting, but the Python process gets terminated along with it.
Thanks in advance!
Upvotes: 0
Views: 1028
Reputation: 25813
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. Further more if you exit the bash session it takes you back into the python interpreter/script.
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. I'm not sure why you're using export but have you considered os.environ['SOMEPATHNAME'] = "/some/path/here"
. This will set the environment variable in your python script before you use subprocess.call
.
Upvotes: 3