首页 > 解决方案 > bash 脚本在它打开的奇异容器中执行操作

问题描述

我在终端的 RedHat 上使用以下工作流程:

singularity run /mn/sarpanitu/singularity/test/fenics-and-more.img

export DISPLAY=:0.0

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

但是,当然,这不起作用,因为导出是在父终端而不是子奇点中完成的(我认为?)。所以我没有以奇异的方式正确导出显示和路径。

有任何解决这个问题的方法吗?我想解决方案是在容器启动时自动运行容器内的脚本,但如何简单地做到这一点?

标签: bashexportsingularity-container

解决方案


知道了!@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

推荐阅读