I want to design a Singularity instance that shuts itself down after some time. The background is that I always want to start an instance (singularity instance start...) when the user executes a command because the process in question (R) might start subprocesses and only if I have an instance, I can make sure that all started processes get terminated when the main process ends.
The instance will run a background process that regularly checks whether there are still processes running in the instance, and if not, should stop the container after some time. The way I could think of to stop an instance is kill 1, because a Singularity instance has its own PID namespace and PID 1 is a process called sinit, which adopts orphaned processes in the container and takes down all processes in the namespace if terminated.
The strange thing is that killing from a subprocess that is started automatically does not work if I kill it from a subprocess of the instance start script. sinit stays.
These are the things that do work (Singularity recipe file and command):
# (test1.def)
Bootstrap: Library
From: alpine:latest
%startscript
sleep 5
kill 1
-------------------------------------------
$ sudo singularity build test1.sif test1.def
$ singularity instance start test1.sif test1
This approach works: The Instance stops itself. But I cant use it because my timeout watcher is a program written in Python, so it is a subprocess. Modelling this gives me a problem:
# (test2.def)
Bootstrap: Library
From: alpine:latest
%startscript
(
sleep 5
kill 1
)
-------------------------------------------
$ sudo singularity build test2.sif test2.def
$ singularity instance start test2.sif test2
Here I have the Problem: The Instance does not stop after 5 seconds.
This, however, works:
$ singularity instance start test2.sif test2
$ singularity exec instance://test2 kill 1
# OK: Instance stops
What I need is the instance test2 stopping itself automatically, not only after I execute
the kill command manually using singularity exec. How can I achieve that?