我需要在Python中使用它自己的专用TTY在一个单独的进程中运行一个交互式Bash实例(我不能使用pexpect).我使用了这个通常在类似程序中使用过的代码片段:
master, slave = pty.openpty()
p = subprocess.Popen(["/bin/bash", "-i"], stdin=slave, stdout=slave, stderr=slave)
os.close(slave)
x = os.read(master, 1026)
print x
subprocess.Popen.kill(p)
os.close(master)
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,我得到以下输出:
$ ./pty_try.py
bash: cannot set terminal process group (10790): Inappropriate ioctl for device
bash: no job control in this shell
Run Code Online (Sandbox Code Playgroud)
运行的Strace显示一些错误:
...
readlink("/usr/bin/python2.7", 0x7ffc8db02510, 4096) = -1 EINVAL (Invalid argument)
...
ioctl(3, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7ffc8db03590) = -1 ENOTTY (Inappropriate ioctl for device)
...
readlink("./pty_try.py", 0x7ffc8db00610, 4096) = -1 EINVAL (Invalid argument)
Run Code Online (Sandbox Code Playgroud)
代码片段看起来很简单,Bash没有得到它需要的东西吗?这可能是什么问题?
如果在shell脚本中我写
chroot /home/mayank/chroot/codebase
cd SBC
Run Code Online (Sandbox Code Playgroud)
当我运行这个 shell 脚本时,它确实进入chroot但不执行命令cd SBC,当我退出时chroot它会执行cd SBC。
如何通过 shell 脚本实现某些功能chroot并执行命令chroot?
我正在尝试使用该模块从 python 内部执行 bash 脚本subprocess。问题是这个 bash 脚本依赖于我的文件中设置的许多环境变量.bashrc。我想要的是 python 子进程能够准确复制启动终端窗口时设置的环境。到目前为止我尝试过:
p = subprocess.Popen(args=['my-script.sh', '--additional_args'], stdout=subprocess.PIPE, env=os.environ.copy(), shell=True)
output_buffer, return_code = p.communicate()
Run Code Online (Sandbox Code Playgroud)
但后来在某处读到,即使在执行完之后shell=True,.bashrc文件仍然没有加载,我应该尝试这样的事情:
p = subprocess.Popen(args=['/bin/bash', '-i', '-c', 'my_script.sh', '--additional_args'], stdout=subprocess.PIPE)
output_buffer, return_code = p.communicate()
Run Code Online (Sandbox Code Playgroud)
但是通过这个函数调用,我得到这个错误:
tput: No value for $TERM and no -T specified
bash: cannot set terminal process group (2071): Inappropriate ioctl for device
bash: no job control in this shell
Run Code Online (Sandbox Code Playgroud)