How to know we are in a singularity shell?

1,013 views
Skip to first unread message

Oriol Guitart

unread,
May 22, 2017, 5:06:32 AM5/22/17
to singularity
Hi all,

In our lab, we have our home directory mounted by nfs. That means, we have the same home for our desktop machines and for our the cluster. The problem is that we use the .bashrc to export some environment variables that are useful when is a normal shell. But these variables might not be useful and produce conflicts when we are in a singularity shell or when executing an script through singularity (singularity exec). I would like to find a way to know when we are in a singularity shell so I can modify my bashrc to export different variables depending on the case. Is there any environment variable specific to singularity that I could check inside my bashrc and make different exports according to that variable ?
Thanks in advance.
Regards,

oriol

Thomas Maier

unread,
May 22, 2017, 5:20:46 AM5/22/17
to singularity
Hi Oriol,

there are several in fact:

(slc)Thomas.Maier@gar-ws-etp07:~>env | grep SINGULARITY
SINGULARITY_libexecdir=/usr/local/libexec
SINGULARITY_localstatedir=/usr/local/var
SINGULARITY_INIT=1
SINGULARITY_sysconfdir=/etc
SINGULARITY_CONTAINER=cern-slc6-05.05.2017.img
SINGULARITY_IMAGE=/project/etp3/ThomasMaier/Singularity/versions/cern-slc6-05.05.2017.img


I'm still using version 2.2.1, so it might look a bit different for newer version, but in general variables beginning with "SINGULARITY" are set by singularity when opening a shell or executing a command within the singularity image (assuming you haven't set it yourself, of course). You might want to check yourself what you get when you're inside the singularity image or running 'singularity exec'.

If one of the devs has a good recommendation for what to use, they can comment as well of course :).

Cheers

Thomas

Oriol Guitart

unread,
May 22, 2017, 6:33:53 AM5/22/17
to singularity
Hi Thomas,

Thanks for your help. Your idea seems good. I actually tried and it could be a solution. However, first of all, I have realized my first assumption was wrong because even if singularity loads my bashrc it also uses the environment variables that were already set from the shell that I'm working on. This can be solved by unseting the variables from the bashrc if I detect I'm starting a singularity shell.  But apparently singularity exec does not load my bashrc before executing. Therefore, I still need to find a solution when I'm not working in the singularity shell but executing just an script through singularity.
Do you know why singularity is not loading the bashrc when using the option 'exec' ? I thought singularity exec was a shortcut to avoid doing singularity shell and then the call to the script.
Regards,

oriol

Thomas Maier

unread,
May 22, 2017, 7:56:31 AM5/22/17
to singularity
Hi Oriol,

generally bash is executing your .bashrc file only if it is executed in interactive mode. If you execute 'singularity shell <image>' it is the same as opening a new terminal window on your machine, i.e. bash is opened as an interactive shell. In fact, it is the same as executing 'singularity exec <image> /bin/bash', i.e. executing bash explicitly (without any additional arguments) inside the image. If you execute 'singularity exec <image> <whatever_command>', this is executed non-interactively, so generally your .bashrc is ignored. In principle you can execute your .bashrc explicitly also when running non-interactively, but usually this is not really desired (often you make setups in .bashrc that require an interactive shell). There are probably plenty of ways to approach this topic (setting env variables for interactive and non-interactive shells). I'd advise that you have a look at the bash documentation regarding this topic:


If there env variables you want to set every time you open bash non-interactively (and not in login mode), you could probably make use of $BASH_ENV as described in the documentation.

Let me know if you have more questions.

Cheers,

Thomas

Oriol Guitart

unread,
May 22, 2017, 8:25:26 AM5/22/17
to singularity
Hi Thomas,

Thanks for all comments. It was very helpful. I didn't realize about the non-interactive mode. It makes sense now.
regards,

oriol

Rémy Dernat

unread,
May 22, 2017, 10:40:28 AM5/22/17
to singu...@lbl.gov
Hi Oriol,

FYI, here is what I am adding in my .bashrc to know/check that:

if [ -n "$SINGULARITY_CONTAINER" ]; then
    PS1=$SINGULARITY_CONTAINER":"$PS1
fi

Hope it could be useful.

Best regards,
Rémy

--
You received this message because you are subscribed to the Google Groups "singularity" group.
To unsubscribe from this group and stop receiving emails from it, send an email to singularity+unsubscribe@lbl.gov.

Reply all
Reply to author
Forward
0 new messages