Tutorials/Slurm-Persistent-Shell: Difference between revisions
Created page with "= Persistent Shell on a Compute Node = ''A follow-up to the '''Interactive Slurm''' series for the HPC Advanced course. It builds on Part 3: salloc — allocations and steps.'' You start something on a compute node, you need to go home, and you want to find it exactly as you left it tomorrow morning: same shell, same history, the program still running. On your own computer you would use <code>tmux</code>: start a session, detach,..." |
m Honfi001 moved page Tutorials/slurm-persistent-shell to Tutorials/Slurm-Persistent-Shell without leaving a redirect |
(No difference)
| |
Latest revision as of 10:28, 23 November 2026
Persistent Shell on a Compute Node
A follow-up to the Interactive Slurm series for the HPC Advanced course. It builds on Part 3: salloc — allocations and steps.
You start something on a compute node, you need to go home, and you want to find it exactly as you left it tomorrow morning: same shell, same history, the program still running. On your own computer you would use tmux: start a session, detach, come back later and attach again.
On the cluster that needs one more idea, because tmux has to run in the right place. This page shows where that is, and how to set it up so that your session survives a lost connection and a login node reboot, and is still cleaned up properly when you are done.
By the end of this page you will be able to:
- explain why
tmuxon the login node, or inside ansrunsession, does not give you a persistent shell on a compute node; - start a
tmuxsession on a compute node from a batch job; - attach to it, detach, lose your connection, and attach again;
- end it so that your resources are released straight away.
You need: Interactive Slurm, Part 3, and basic tmux: start a session, detach with Ctrl-b d, attach again.
Time: about 30 minutes.
Why the obvious ways do not work
There are three obvious places to put tmux, and each one breaks in a different way:
| Where tmux runs | What goes wrong |
|---|---|
| on the login node | The login node is not a compute node, so your work does not belong there. And login nodes are rebooted now and then for maintenance, which ends every tmux session on them.
|
inside an interactive shell (srun --pty bash, sinteractive) |
That shell is a job step. When it ends, Slurm ends everything started inside it, tmux included. Part 3 shows this happening.
|
on the login node, running srun --pty bash inside it |
Survives your laptop disconnecting, but the srun that holds the step open lives on the login node, so a login node reboot ends the step, and everything in it.
|
They all fail for one reason. Here it is, the most useful sentence on this page:
A job step lives only as long as the program that launched it.
Every srun you type is launched from the login node, so every step it creates is tied to something on the login node: your connection, your terminal, or the login node itself. To get a session that outlives all of those, it has to be launched by something that is not on the login node.
The idea: let Slurm start tmux for you
When you submit a batch job with sbatch, Slurm starts your script on the compute node itself, as a step called batch. Nothing of yours stays behind on the login node, so there is nothing there to lose.
So the recipe is:
- a small batch job whose only work is to start a
tmuxsession on the compute node and keep the job alive while that session exists; - a throwaway
srunstep each time you want to look at it, to attach.
The attaching step can die whenever it likes, because the session does not live in it.
Step 1: The batch script
Save this as tmux-work.sh:
#!/bin/bash
#SBATCH --job-name=tmux-work
#SBATCH --time=00:10:00
#SBATCH --cpus-per-task=1
tmux new-session -d -s work
while tmux has-session -t work 2>/dev/null; do sleep 30; done
Line by line:
- the
#SBATCHlines are the same options you givesrun, written into the script: a name, a time limit, and the cores; tmux new-session -d -s workstarts atmuxsession calledwork, detached (-d), so it runs without a screen attached;- the
whileloop checks every 30 seconds whether the session still exists. As long as it does, the script keeps running, and so does your job. When you close the session, the loop ends, the script ends, and the job ends by itself.
This is the exact script used for every example on this page. For real work, ask for what you need: in particular, add #SBATCH --mem=4G (or however much you need), because without it you get the default of 100 MB, and set --time to how long you will really use it.
Submit it:
sbatch tmux-work.sh
Submitted batch job 44379814
Then wait until it is running, and note the job ID:
squeue -j 44379814
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON)
44379814 main tmux-wor honfi001 R 0:05 1 node250
R means running, on node250. Your session is now waiting for you there.
Step 2: Look in, then attach
Before attaching, we looked at the session from outside, with a short srun step. It does four things: lists the tmux sessions, asks Linux which part of Slurm the tmux server belongs to (its cgroup), asks the same about itself, and types a clock into the session so that we have something to watch later:
srun --jobid=44379814 --overlap bash -c '
date -Is; tmux ls
P=$(tmux display -p -t work "#{pid}"); echo server pid $P; cat /proc/$P/cgroup
echo this step:; cat /proc/self/cgroup
tmux send-keys -t work "while true; do date +%T; sleep 5; done" Enter'
2026-11-23T10:01:56+01:00
work: 1 windows (created Mon Nov 23 10:01:43 2026)
server pid 1532493
0::/system.slice/slurmstepd.scope/job_44379814/step_batch/user/task_0
this step:
0::/system.slice/slurmstepd.scope/job_44379814/step_0/user/task_0
Keep the two 0:: lines in mind: they explain everything in Step 3. You do not need any of this for normal use. Normally you just attach:
srun --jobid=44379814 --overlap --pty tmux attach -t work
This is a new step in your job, as in Part 3: --jobid puts it in the job you already have, --overlap lets it share the job's cores with any other step that is running, such as an attach you still have open in another terminal, and --pty connects your keyboard and screen. The program it runs is tmux attach.
You are now in the tmux session on node250, with the [work] status bar at the bottom and the clock ticking every five seconds. Work as you normally would.
Step 3: Leave, the hard way
The polite way to leave is to detach with Ctrl-b d. We tested the rude way: while attached, we killed the connection from outside, as if the network had dropped or the laptop lid had closed. The clock had printed up to 10:02:31 when the connection went at 10:02:33.
Then, from a fresh login, we looked at the session from another step:
srun --jobid=44379814 --overlap bash -c "tmux ls; tmux capture-pane -p -t work | grep -v '^$' | tail -4"
work: 1 windows (created Mon Nov 23 10:01:43 2026)
10:02:26
10:02:31
10:02:36
10:02:41
The session is still there, and the clock kept ticking after the connection was gone: 10:02:36 and 10:02:41 were printed with nobody attached. (tmux capture-pane -p prints what is on the session's screen. Normally you would simply attach again and look.)
Why it survived
Go back to the two 0:: lines from Step 2. The first is the tmux server: job 44379814, step batch. It lives in the batch step, which Slurm started on the compute node, and which ends only when your script ends. The second line is the step we asked from, a throwaway srun step, like every attach. When an attaching step dies, only that step ends.
Being inside the job also means that everything you run in the session uses the cores your job was given, and that it cannot outlive the job: when the job ends, for any reason, Slurm ends everything in it.
Step 4: Finish, and give the resources back
When you are done, close the session: type exit in every window, or run tmux kill-session -t work. We did it from outside:
srun --jobid=44379814 --overlap tmux kill-session -t work
Within 30 seconds the loop in the script notices, the script ends, and the job ends with it. There is nothing to cancel. Here is the whole job afterwards:
sacct -j 44379814 -o JobID%15,JobName%10,End,Elapsed,State,ExitCode
JobID JobName End Elapsed State ExitCode
--------------- ---------- ------------------- ---------- ---------- --------
44379814 tmux-work 2026-11-23T10:03:13 00:01:30 COMPLETED 0:0
44379814.batch batch 2026-11-23T10:03:13 00:01:30 COMPLETED 0:0
44379814.extern extern 2026-11-23T10:03:13 00:01:30 COMPLETED 0:0
44379814.0 bash 2026-11-23T10:01:56 00:00:04 COMPLETED 0:0
44379814.1 tmux 2026-11-23T10:02:05 00:00:03 FAILED 1:0
44379814.2 tmux 2026-11-23T10:02:33 00:00:19 CANCELLED+ 0:9
44379814.3 bash 2026-11-23T10:02:44 00:00:04 COMPLETED 0:0
44379814.4 tmux 2026-11-23T10:02:55 00:00:04 COMPLETED 0:0
Everything from this page is in there:
batchis your script, holding thetmuxsession. It ran for the whole job and endedCOMPLETEDwhen the session closed;.0and.3are the two times we looked in from outside: the check in Step 2, and thecapture-panein Step 3;.1is a failed attach: our test terminal had no terminal type set (see Troubleshooting);.2is the attach whose connection we killed:CANCELLED, by signal 9. The step died, and the session did not care;.4is thekill-session.
Every attach is a new step, so you can count your own reconnects in sacct.
Other ways it ends: scancel <job ID> ends it at once, and so does reaching the time limit, with your session and everything running in it. Save your work before the time runs out.
Be a good neighbour. While the session exists, the job holds its cores and memory, whether you are using them or not, and nobody else can use them. Ask for an honest --time, and close the session when you are done rather than leaving it to run out.
How it compares
| Approach | Survives a lost connection? | Survives a login node reboot? | Ends cleanly with the job? |
|---|---|---|---|
tmux on the login node |
yes | no | not a job at all |
tmux inside srun --pty bash or sinteractive |
no | no | yes |
salloc --no-shell + srun --jobid --overlap --pty bash |
the allocation does, the shell does not | the allocation does, the shell does not | yes |
salloc --no-shell + ssh to the node + tmux |
yes | yes | not at the moment: see the warning in Part 3 |
sbatch + tmux (this page) |
yes | yes | yes |
Try it yourself
This makes a good exercise for a workshop.
- Copy the script, add
#SBATCH --mem=1G, and submit it. Wait forRinsqueue --me. - Attach, and start something long in the session, such as the clock above.
- Do not detach politely: close your terminal window, or switch off your Wi-Fi for a moment.
- Log in again and attach again. Is the clock still running? Did it miss any seconds?
- Open a second window in the session (
Ctrl-b c) and runnproc. Does it match what you asked for? - Close the session. Check with
squeue --methat the job has gone, then readsacct -j <job ID>and explain every line.
Troubleshooting
open terminal failed: terminal does not support cleartmuxdoes not know what kind of terminal you have, because theTERMvariable is unset or set todumb. Most terminal programs set it for you. If yours does not, give it one when you attach:TERM=xterm-256color srun --jobid=<job ID> --overlap --pty tmux attach -t work.
no server runningorcan't find session: work- The session has ended, and so has the job. Check with
squeue --me, and look atsacct -j <job ID>for the reason (the time limit, for example).
- The attach just waits, with no message
- You probably left out
--overlapwhile another step is running in the job, for example because you are still attached in another terminal. Without--overlap, a new step waits until the cores are free, and it waits silently. We tested it: with a step already running, ansrun --jobid=... tmux lswithout--overlapprinted nothing for 20 seconds, until we stopped it. The same command with--overlapanswered at once. (Thebatchstep itself does not block you. A first attach works even without--overlap. It is the second one that hangs.) PressCtrl-Cand attach again with--overlap.
Cheat sheet
| You want to... | Type |
|---|---|
| start the session | sbatch tmux-work.sh
|
| check it is running | squeue --me
|
| attach | srun --jobid=<job ID> --overlap --pty tmux attach -t work
|
| detach | Ctrl-b d
|
| end it and free the resources | exit in every window, or tmux kill-session -t work
|
| end it from outside | scancel <job ID>
|