Jump to content

Tutorials/Slurm-Interactive-Salloc

From HPCwiki
Revision as of 10:58, 23 November 2026 by Honfi001 (talk | contribs) (Created page with "= Interactive Slurm, Part 3: salloc — allocations and steps = ''Part 3 of the '''Interactive Slurm''' series for the HPC Advanced course. Part 1 covers <code>srun</code>, and Part 2 covers interactive shells.'' So far, every interactive session in this series has been tied to one terminal. Close the terminal, lose your connection, or have the login node rebooted under you, and the session is...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Interactive Slurm, Part 3: salloc — allocations and steps

Part 3 of the Interactive Slurm series for the HPC Advanced course. Part 1 covers srun, and Part 2 covers interactive shells.

So far, every interactive session in this series has been tied to one terminal. Close the terminal, lose your connection, or have the login node rebooted under you, and the session is gone, along with the resources you waited for. This page pulls apart the two things srun has been doing together all along, and shows how to keep a session on a compute node that you can leave and come back to.

By the end of this page you will be able to:

  • explain the difference between an allocation and a job step;
  • book resources with salloc and run steps inside them;
  • hold an allocation with no terminal attached, and connect to it when you like;
  • keep a tmux session on a compute node across disconnects;
  • release what you booked when you are done.

You need: Part 1 and Part 2, and basic tmux: start a session, detach with Ctrl-b d, attach again.

Time: about 45 minutes.

Allocations and steps

Back to the restaurant from Part 1. An allocation is your table: cores, memory and time on one or more nodes, reserved for you. A step is one dish: one program started inside the allocation.

srun on the login node books a table, orders one dish, and leaves as soon as it is eaten. You saw that in Part 2, where sacct showed every session as a job with exactly one step, .0.

salloc does only the first half. It books the table and orders nothing. What you order, and when, is up to you, and every srun you run inside the allocation becomes a new step at the same table, with no new wait in the queue.

Step 1: salloc

salloc --time=00:05:00 -c 1
salloc: Pending job allocation 44376930
salloc: job 44376930 queued and waiting for resources
salloc: job 44376930 has been allocated resources
salloc: Granted job allocation 44376930
salloc: Nodes node114 are ready for job
honfi001@login200:~$ hostname
login200
honfi001@login200:~$ echo $SLURM_JOB_ID
44376930
honfi001@login200:~$ srun hostname
node114

Read the prompt carefully, because this is the part that surprises people. You got an allocation on node114, but your shell is still on the login node (login200). On Anunna, salloc gives you a shell on the login node that knows about your allocation ($SLURM_JOB_ID is set), and nothing more. Some other clusters drop you onto the compute node instead, so do not take this for granted elsewhere.

So in this shell:

  • a plain command, such as hostname, runs on the login node, like any other command there. Do not run heavy work this way;
  • a command started with srun runs on your allocation, as a new step, straight away.

The table is yours until you leave

An allocation from salloc lasts as long as the salloc shell does, whether or not anything is running in it. Here is the session above, seen in sacct:

sacct -j 44376930 -o JobID%14,JobName%10,Elapsed,State,End
         JobID    JobName    Elapsed      State                 End
-------------- ---------- ---------- ---------- -------------------
      44376930 interacti+   00:04:57  COMPLETED 2026-11-23T05:59:23
44376930.exte+     extern   00:04:57  COMPLETED 2026-11-23T05:59:23
    44376930.0   hostname   00:00:05  COMPLETED 2026-11-23T05:54:32

The one real piece of work, step .0 (hostname), took five seconds. The allocation, the first line, lasted almost five minutes: the shell sat there doing nothing until its connection closed, and the core on node114 was held for nobody the whole time. Type exit as soon as you are done, because an idle allocation still counts against you and still keeps a core from someone in the queue.

The other side of the same fact: the allocation ends when the shell ends. This salloc shell lives on the login node, so if your connection drops, or the login node is rebooted for maintenance, the shell goes and your allocation is released.

Step 2: Book a table without a shell

You can ask salloc for the allocation alone, with no shell attached:

salloc --no-shell --time=00:15:00 -c 1 -J persist-test
salloc: Pending job allocation 44376933
salloc: job 44376933 queued and waiting for resources
salloc: job 44376933 has been allocated resources
salloc: Granted job allocation 44376933
salloc: Nodes node114 are ready for job

And you are back at your prompt. -J persist-test just gives the job a name, so that you can recognise it. Nothing of yours is running anywhere, but the table is booked. You can log out completely, and it is still there when you come back:

squeue -j 44376933 -o "%.10i %.13j %.2t %.8M %.9l %R"
     JOBID          NAME ST     TIME TIME_LIMI NODELIST(REASON)
  44376933  persist-test  R     0:05     15:00 node114

That output came from a fresh login, after the one that ran salloc had closed. R means running, and NODELIST tells you which node your table is on. You will need that in Step 4.

An allocation with no shell does not end by itself when you are done. It runs until its time limit, or until you cancel it (Step 5). Give it an honest --time.

Step 3: Connect with srun, and why that is not enough

To get a shell inside that allocation, run a step into it with srun:

srun --jobid=44376933 --overlap --pty bash -i
  • --jobid: put this step into the allocation I already have, instead of booking a new one;
  • --overlap: let this step share the allocation's cores with other steps, so that you can open a second terminal into the same job while the first one is still open;
  • --pty bash -i: the interactive shell from Part 2.

Here we connected, started a tmux session, and left:

honfi001@node114:~$ hostname
node114
honfi001@node114:~$ tmux new -d -s t1 'sleep 600'
honfi001@node114:~$ tmux ls
t1: 1 windows (created Mon Nov 23 06:00:08 2026)
honfi001@node114:~$ exit
exit

Then we looked for the tmux session from another step:

srun --jobid=44376933 --overlap bash -c 'tmux ls; ps -u $USER -o pid,lstart,cmd | grep -E "tmux|sleep" | grep -v grep'
no server running on /tmp/tmux-17403473/default
srun: error: node114: task 0: Exited with exit code 1

Gone. No tmux session, and ps finds no tmux or sleep process at all (the error line just means grep found nothing). The allocation was still there, but the tmux session was not.

The reason is the most useful idea on this page: when a step ends, Slurm ends everything that was started inside it. Your bash was a step. tmux was started from that bash, so it belonged to the same step, and when you typed exit the step ended and took tmux with it. This is deliberate. It is how Slurm makes sure nothing is left behind on a node. It also means no step is a safe place for something you want to keep running: not tmux, not screen, not nohup.

Step 4: A tmux session that survives

The recommended way to keep a persistent shell on a compute node is a batch job running tmux, which has its own tutorial: Persistent Shell on a Compute Node. It needs nothing beyond what you have learned here, and it is cleaned up properly when the job ends. This section shows the other way, through SSH, because it is useful to know and because it shows the same idea from a different side.

Anunna lets you ssh straight into a compute node, but only while you have a job running on it. Your SSH session is not a step, so it is not ended when a step ends. It is meant to belong to the job as a whole (but read the warning below).

So the pattern is:

  1. Book the table: salloc --no-shell --time=02:00:00 -c 2 --mem=4G
  2. Find the node: squeue --me, and read NODELIST.
  3. Log in to it: ssh node114. The first time you connect to a node, SSH asks whether you trust it. Answer yes.
  4. Start tmux there: tmux new -s work, and work inside it.
  5. Detach with Ctrl-b d, log out, close your laptop, go home.
  6. Come back: ssh to the login node, ssh node114, tmux attach -t work.

Here is what that looks like, run as single commands from the login node. (We added options so that SSH would not stop to ask the trust question. On your first connection you will see that question instead of the "Permanently added" line.)

ssh node114 "hostname; cat /proc/self/cgroup; tmux new -d -s t2 'sleep 600'; tmux ls"
Warning: Permanently added 'node114' (ED25519) to the list of known hosts.
node114
0::/user.slice/user-17403473.slice/session-2082.scope
t2: 1 windows (created Mon Nov 23 06:06:45 2026)

That SSH connection closed when the command finished. A new one, fifteen seconds later:

ssh node114 'tmux ls'
t2: 1 windows (created Mon Nov 23 06:06:45 2026)

The session survived the disconnect. This is the one that keeps going if your connection drops or the login node is rebooted: it does not live on the login node, and it does not live in a step.

Warning — a known problem on Anunna, being fixed. Your SSH session should belong to your job, so that everything you start in it ends when the job ends. At the moment it does not. Look at the cgroup line above: it names a login session (session-2082.scope), not the job. As a result, anything you start over SSH on a compute node keeps running after your job has ended, outside the cores and memory you were given, until someone stops it.

Until this is fixed:

  • end your tmux session yourself (type exit in every window, or run tmux kill-session -t work) before you cancel your job or its time runs out;
  • run heavy work inside the session with srun --jobid=<job ID> --overlap ..., so that it stays within your allocation;
  • do not use this to keep things running after your job. That is what sbatch is for.

Once it is fixed, the tmux session will end together with the job, as it should, and this box will go.

Step 5: Give the table back

An allocation from salloc --no-shell keeps its resources until its time runs out. When you are done, cancel it:

scancel 44376933

After that, the node no longer lets you in:

ssh node114
Access denied by pam_slurm_adopt: you have no active jobs on this node
Connection closed by 192.168.0.78 port 22

That message is the "only while you have a job" rule from Step 4 at work.

Which one should I use?

Command Where your shell runs Survives a lost connection? The allocation ends when...
srun command no shell, one command no the command finishes
srun --pty bash -i, sinteractive compute node no you exit the shell
salloc login node no you exit the shell
salloc --no-shell + srun --jobid --overlap --pty bash -i compute node, as a step the allocation does; the shell and anything started in it do not you scancel it, or time runs out
salloc --no-shell + ssh + tmux compute node, in the job yes you scancel it, or time runs out (see the warning above)

For a shell on a compute node that survives everything, including a login node reboot, and still ends cleanly with the job, see Persistent Shell on a Compute Node.

And when you no longer need to watch it at all, write a batch script and use sbatch. Interactive sessions are for exploring and debugging, and batch jobs are for work.

Try it yourself

  1. Run salloc --time=00:10:00 -c 2. In the shell, compare hostname with srun hostname, and nproc with srun nproc. Then exit, and look at the steps with sacct -j <job ID>.
  2. Book an allocation with --no-shell, connect twice from two terminals with srun --jobid=... --overlap --pty bash -i, and check that both prompts show the same node and the same $SLURM_JOB_ID.
  3. Repeat Step 3 for yourself: start tmux inside an srun step, leave the step, and check whether tmux survived.
  4. Follow the pattern in Step 4 with a short --time. Detach, log out, log back in and attach again. Then end the tmux session and scancel the job.

Cheat sheet

You want to... Type
book resources with a shell (on the login node) salloc --time=01:00:00 -c 2 --mem=4G
book resources with no shell salloc --no-shell --time=01:00:00 -c 2 --mem=4G
see your jobs and their nodes squeue --me
open a shell in an existing allocation srun --jobid=<job ID> --overlap --pty bash -i
run a command in an existing allocation srun --jobid=<job ID> --overlap command
log in to your job's node ssh <node>
keep a session across disconnects tmux new -s work, detach with Ctrl-b d, tmux attach -t work
release the allocation scancel <job ID>

See also