execve 因参数错误而失败
我想用交互式bash shell替换我的程序。在Golang(使用os/exec.command
)和Python(OS.System
)启动Works Works,但我想使用syscall.exec( )
或os.execve()
。
演示Python代码:
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)
Env.SH的内容:
export PS1='interactive shell $ '
echo inside env.sh
ls -la /proc/self/fd
Python输出:
$ 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
如何调试为什么Bash在第二次运行中会退出?
I want to replace my program with an interactive Bash shell. Launching works in Golang (with os/exec.Command
) and Python (os.system
) but not when I want to replace the process using syscall.Exec()
or 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
Any ideas how to debug why Bash would exit at the second run?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它是
execve(“/bin/bash”,[“/bin/bash”,“ - rcfile”,...])
。您正在运行
bash env.sh -i
- 用一个参数-i
执行脚本env.sh
,然后将过程名称设置为-rcfile
。It's
execve("/bin/bash", ["/bin/bash", "--rcfile", ...])
.You are running
bash env.sh -i
- executing scriptenv.sh
with one argument-i
, and process name set to--rcfile
.