我想用一个交互式Bash shell来替换我的程序。在Golang(使用
os/exec.Command
)和Python(
os.system
)中启动是可行的,但当我想用
syscall.Exec()
或
os.execve()
来替换进程时就不行了。
Demo Python code:
import os
# first try with fork: OK
os.system("/bin/bash --rcfile env.sh -i")
# second try with execve: FAIL (exit after reading env.sh)
os.execve("/bin/bash", ["--rcfile", "env.sh", "-i"], os.environ)
Contents of env.sh:
export PS1='interactive shell $ '
echo inside env.sh
ls -la /proc/self/fd
Python output:
$ echo "Start: $$"; python test.py; echo "End: $$";
Start: 684875
inside env.sh
total 0
lrwx------ 1 willem willem 64 mrt 31 14:03 0 -> /dev/pts/1
lrwx------ 1 willem willem 64 mrt 31 14:03 1 -> /dev/pts/1
lrwx------ 1 willem willem 64 mrt 31 14:03 2 -> /dev/pts/1
lr-x------ 1 willem willem 64 mrt 31 14:03 3 -> /proc/737221/fd
interactive shell $ exit <--- Control-D from me
inside env.sh
total 0
lrwx------ 1 willem willem 64 mrt 31 14:03 0 -> /dev/pts/1
lrwx------ 1 willem willem 64 mrt 31 14:03 1 -> /dev/pts/1
lrwx------ 1 willem willem 64 mrt 31 14:03 2 -> /dev/pts/1
lr-x------ 1 willem willem 64 mrt 31 14:03 3 -> /proc/737223/fd
End: 684875