bash 用于在其打开的奇点容器中执行操作的脚本
bash script to perform action in a singularity container it opens
我在终端的 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
但是,当然,这不起作用,因为导出是在父终端而不是子奇点中完成的(我认为?)。所以我没有在奇异点正确导出显示和路径。
有什么办法可以解决这个问题吗?我想解决方案是在容器启动时自动 运行 容器内的脚本,但如何简单地做到这一点?
如果您有权更改 Singularity 图像(或重建它),则可以更改 runscript. Otherwise you could put your bash script at a location that is mounted inside the Singularity image (probably the current working directory at least depending on your setup). Then run something like this (using exec)
singularity exec my_sing.img bash /pth/to/script.sh
知道了! @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"
其中 singularity_bashrc
是您要使用的 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)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[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
我在终端的 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
但是,当然,这不起作用,因为导出是在父终端而不是子奇点中完成的(我认为?)。所以我没有在奇异点正确导出显示和路径。
有什么办法可以解决这个问题吗?我想解决方案是在容器启动时自动 运行 容器内的脚本,但如何简单地做到这一点?
如果您有权更改 Singularity 图像(或重建它),则可以更改 runscript. Otherwise you could put your bash script at a location that is mounted inside the Singularity image (probably the current working directory at least depending on your setup). Then run something like this (using exec)
singularity exec my_sing.img bash /pth/to/script.sh
知道了! @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"
其中 singularity_bashrc
是您要使用的 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)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[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