bash - bash 脚本在它打开的奇异容器中执行操作
问题描述
我在终端的 RedHat 上使用以下工作流程:
- 打开一个奇点图像:
singularity run /mn/sarpanitu/singularity/test/fenics-and-more.img
- 导出奇点内的一些显示:
export DISPLAY=:0.0
- 在奇点内导出到gmsh的路径:
export PATH="$HOME/Downloads/gmsh-git-Linux64/bin:$PATH"
我想把所有东西都写成 bash 脚本。我的第一个(不工作)方法如下(全部在一个singularity_script.sh文件中):
#!/bin/bash
function singularity_script(){
singularity run /mn/sarpanitu/singularity/test/fenics-and-more.img
export DISPLAY=:0.0
export PATH="$HOME/Downloads/gmsh-git-Linux64/bin:$PATH"
}
我通过采购然后调用函数来执行它:
chmod +x singularity_script.sh
. singularity_script.sh
singularity_script
但是,当然,这不起作用,因为导出是在父终端而不是子奇点中完成的(我认为?)。所以我没有以奇异的方式正确导出显示和路径。
有任何解决这个问题的方法吗?我想解决方案是在容器启动时自动运行容器内的脚本,但如何简单地做到这一点?
解决方案
知道了!@tormodlandet 的解决方案有一个问题,即一旦使用该-c
选项调用的命令运行,奇点容器就会死亡。
我可以通过执行以下命令来使其工作:
singularity shell /mn/sarpanitu/singularity/test/fenics-and-more.img -c "export DISPLAY=:0.0 && export PATH="$HOME/Downloads/gmsh-git-Linux64/bin:$PATH" && /bin/bash -norc"
这会在奇点中执行我想要的命令,然后gnome-terminal
从奇点容器中生成一个在退出之前不会死亡的容器。
缺少/bin/bash -norc
最后的 意味着奇点容器在最后一个命令之后死亡。
因此,为了从脚本而不是普通命令中调用有用的命令,只需使用:
singularity shell /mn/sarpanitu/singularity/test/fenics-and-more.img -c "bash script_singularity.sh && /bin/bash -norc"
当前工作目录中有一个script_singularity.sh
包含要运行的命令的文件:
echo "Hi there..."
export DISPLAY=:0.0
export PATH="$HOME/Download/gmsh-git-Linux64/bin:$PATH"
echo "...done exports!"
编辑
如果你还想拥有一个好看的终端,你可以提供一个bashrc
配置文件。例如:
singularity shell /mn/sarpanitu/singularity/test/fenics-and-more.img -c "bash script_singularity.sh && /bin/bash -rcfile singularity_bashrc"
你的 bashrc在哪里singularity_bashrc
使用。例如,它很好用(在这里添加很冗长,并且有很多地方有更详细的 bashrc 解释,但在一些评论中要求这样做):
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "${debian_chroot:-}" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="sing-\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
推荐阅读
- wcf - 无法将 Wcf Web 服务更新或添加到 Windows 通用应用程序 UWP
- r - 如何让这个函数在 R 中的多个内核/处理器上运行?
- python - 为什么直接向 numpy 数组添加新属性不起作用,但通过子类化这样做?
- python - 集成的 dash-app(使用 django_plotly_dash)没有获得足够的空间
- java - 代码重构:对 Resttemplate 的 CloseableHttpResponse
- woocommerce - Woocommerce 获取产品类别路径
- java - 如何从两种不同类型的列表创建对象列表
- javascript - Uncaught (in promise) TypeError: this.getChildrem is not a function
- arduino - 最大化 Arduino Nano 33 BLE Sense 的 Serial.write() 数据传输速度
- python - 加载 tensorflow 模型时出错 - TypeError: __init__() got an unexpected keyword argument 'axis'