Tutorials/Slurm-Interactive-Shells
Interactive Slurm, Part 2: srun --pty bash and sinteractive
Part 2 of the Interactive Slurm series for the HPC Advanced course. Part 1 covers srun, and Part 3 covers salloc.
In Part 1 every srun ran one command and gave the compute node straight back. That is perfect for a single command, and tiresome when you want to look around, try something, fix it and try again. This page shows how to get a whole shell on a compute node, so that you can type one command after another there, and how sinteractive fits in.
By the end of this page you will be able to:
- open an interactive shell on a compute node with
srun; - explain what
sinteractivedoes, because you will have read all of it; - recognise the defaults you get when you do not ask, and why you should always ask;
- see your job and its steps in
sacct.
You need: Part 1.
Time: about 30 minutes.
Step 1: A shell on a compute node
Here is the command:
srun --time=00:05:00 -c 2 --pty bash -i
You already know --time and -c. The new parts are:
bash: the program to run on the compute node is a shell, the same program that reads your commands on the login node;--pty: connect your keyboard and screen to that program, so that it behaves like a normal terminal;-i: tellbashit is interactive, so you get a prompt.
In restaurant terms from Part 1: you book a table, and the one dish you order is "a waiter who stays at my table and takes orders until I leave".
Here is a real session. The lines after the prompt ($) are what we typed:
srun: job 44376925 queued and waiting for resources
srun: job 44376925 has been allocated resources
user001@node114:~$ hostname
node114
user001@node114:~$ nproc
2
user001@node114:~$ echo $SLURM_JOB_ID
44376925
user001@node114:~$ exit
exit
Look at the prompt: it says node114, not the login node. Everything you type now runs on the compute node, with the two cores you asked for, until you type exit or press Ctrl-D. Then you are back on the login node, and the allocation is released.
Your terminal must stay open. The shell lives only as long as your connection to it. If you close your laptop or your connection drops, the shell ends and the allocation is released. Part 3 is about what to do when that is a problem.
Step 2: What did you actually get?
Inside a job, Slurm can tell you exactly what it reserved for you:
scontrol show job $SLURM_JOB_ID | grep -o 'TRES=[^ ]*'
TRES=cpu=2,mem=100M,node=1,billing=2
TRES=cpu=2,mem=100M,node=1,billing=2
(It prints twice: once for what you asked for and once for what you were given.) Two CPUs, as requested, and one node. But look at the memory: 100M, which is 100 megabytes. We never asked for memory, so we got the default.
The defaults are small on purpose
On Anunna's default partition, a job that does not say what it needs gets:
| Resource | Default if you do not ask | Ask with |
|---|---|---|
| CPU cores | 1 | -c 4
|
| Memory | 100 MB | --mem=4G
|
| Time | 1 hour | --time=02:00:00
|
100 MB is enough for hostname and nproc, and too little for most real work: many programs need more than that just to start, and loading a dataset into R or Python will need far more. So for anything beyond a quick look, always say how much memory you need, as well as cores and time:
srun --time=01:00:00 -c 2 --mem=4G --pty bash -i
Step 3: sinteractive is srun
Anunna has a command called sinteractive. It is not part of Slurm. It is a small script written for this cluster, and here is the whole thing:
#!/bin/bash
#Written by Gwen Dawes, 050117
#Why isn't this in the base install?
srun "$@" -I60 -N 1 -n 1 --pty bash -i
One line of real work. "$@" means "whatever options you typed", and the rest you can now read yourself:
| Part | Meaning |
|---|---|
"$@" |
your options, such as -c 2 --mem=4G --time=01:00:00
|
-I60 |
short for --immediate=60: if Slurm cannot find room within 60 seconds, give up instead of waiting in the queue
|
-N 1 -n 1 |
one node, one task: a single shell |
--pty bash -i |
exactly what you typed in Step 1 |
So these two commands do the same job, except that sinteractive gives up after a minute if the cluster is busy:
sinteractive --time=00:05:00 -c 2
srun --time=00:05:00 -c 2 -N 1 -n 1 --pty bash -i
And here is the sinteractive session (shortened to the interesting lines):
srun: job 44376926 queued and waiting for resources
srun: job 44376926 has been allocated resources
user@node114:~$ nproc
2
user001@node114:~$ scontrol show job $SLURM_JOB_ID | grep -o 'TRES=[^ ]*'
TRES=cpu=2,mem=100M,node=1,billing=2
TRES=cpu=2,mem=100M,node=1,billing=2
user001@node114:~$ exit
exit
Even the messages come from srun, because srun is what is running. And typing just sinteractive, with no options at all, gets you exactly the defaults from the table above (again shortened):
srun: job 44376927 queued and waiting for resources
srun: job 44376927 has been allocated resources
user001@node114:~$ nproc
1
user001@node114:~$ scontrol show job $SLURM_JOB_ID | grep -o 'TRES=[^ ]*'
TRES=cpu=1,mem=100M,node=1,billing=1
TRES=cpu=1,mem=100M,node=1,billing=1
user001@node114:~$ exit
exit
One core, 100 MB, and (as sacct will show in a moment) one hour.
Which should I use?
sinteractive |
srun --pty bash -i
| |
|---|---|---|
| Exists on | this cluster (a local script) | every Slurm cluster |
| If the cluster is busy | gives up after 60 seconds | waits in the queue until it starts |
| Number of tasks and nodes | one, set by the script | your choice |
| Shorter to type | yes | no |
Use sinteractive for everyday work on Anunna. Knowing that it is srun underneath means that you can still work when you move to another cluster, and that you can make it wait in the queue when you need a large node and a minute is not enough.
Step 4: Your session, seen from outside
After you leave, sacct shows what happened. Here are the three sessions from this page:
sacct -j 44376925,44376926,44376927 -o JobID%14,JobName%10,Timelimit,Elapsed,State,NodeList
JobID JobName Timelimit Elapsed State NodeList
-------------- ---------- ---------- ---------- ---------- ---------------
44376925 bash 00:05:00 00:00:09 COMPLETED node114
44376925.exte+ extern 00:00:09 COMPLETED node114
44376925.0 bash 00:00:08 COMPLETED node114
44376926 bash 00:05:00 00:00:07 COMPLETED node114
44376926.exte+ extern 00:00:08 COMPLETED node114
44376926.0 bash 00:00:08 COMPLETED node114
44376927 bash 01:00:00 00:00:08 COMPLETED node114
44376927.exte+ extern 00:00:08 COMPLETED node114
44376927.0 bash 00:00:08 COMPLETED node114
Three things to notice:
- Each session is a job with a step. The plain line (
44376925) is the allocation, the table. The line ending in.0is the step that ran inside it, yourbash. This is the "one allocation plus one step" from Part 1, made visible. (Theexternline is Slurm's own bookkeeping step for processes that join the job from outside. You can ignore it here.) - Leaving ends the allocation. We booked five minutes, used about eight seconds, and the job ended the moment we typed
exit. It did not sit there holding the node for the rest of the five minutes. - The bare
sinteractivegot a one-hour limit (01:00:00), which is the default.
Try it yourself
- Open a shell with
srun --time=00:10:00 -c 2 --mem=1G --pty bash -i. Checkhostname,nproc, and theTRESline. Does the memory match what you asked for? - Inside that shell, run
srun hostname. What happens, and how many cores does it use? (Hint: you are already inside an allocation.) - Exit, then run
sacct -j <your job ID>. How many steps do you see now, and why? - Run
sinteractivewith no options and check what you got. Would that be enough to load a 2 GB file?
Cheat sheet
| You want to... | Type |
|---|---|
| a shell on a compute node (Anunna) | sinteractive --time=01:00:00 -c 2 --mem=4G
|
| the same on any Slurm cluster | srun --time=01:00:00 -c 2 --mem=4G --pty bash -i
|
| check what you were given | scontrol show job $SLURM_JOB_ID
|
| leave and release the node | exit or Ctrl-D
|
| see your job and its steps afterwards | sacct -j <job ID>
|
Next
Everything so far ties the allocation to one connection: when your shell ends, your table is given back. In Part 3 you will pull those apart with salloc: book the table first, then order as many steps as you like, from as many terminals as you like.
See also
- Interactive Slurm, Part 1: srun
- Interactive sessions — the beginner tutorial
- Interactive Jobs — reference page for
sinteractiveandsalloc