Watching a job run: interactive sessions, tmux, htop and ps
Last updated on 2026-11-23 | Edit this page
Overview
Questions
- How do I get a shell on a compute node to try things out or to watch a program run?
- How can I keep that session alive if my connection drops, and look at two things at once?
- How can I see, while a program runs, how many CPUs it is really using?
Objectives
- Start an interactive session on a compute node and check what it gives you.
- Use
tmuxpanes to run a program and monitor it side by side, and reattach after a disconnection. - Read
htopandpsoutput to see how many CPUs a program is using at a given moment. - Recognise the point where our script changes from parallel to serial.
In the last two episodes we looked at jobs after they finished. Now we watch one while it runs. We need three things:
- a shell on a compute node, so that we do real work in the right place (an interactive session);
- a way to keep that session alive and to see two things at once
(
tmux); - tools to look at what the program is doing (
htopandps).
An interactive session
A batch job runs a script and ends. An interactive session gives you a shell on a compute node, with the CPUs, memory and time you asked for, in which you type commands as you would on the login node. It is the right way to test a command, to debug, and to watch a program.
OUTPUT
srun: job 853340 queued and waiting for resources
srun: job 853340 has been allocated resources
[c.myuser@<node> ~]$
The request has the same ingredients as a job script, now on the
command line (-c 8 is eight CPUs for one task, as in the
earlier episodes), and --pty bash asks for a shell. If the
cluster is busy you wait in the queue, as with any job. When the prompt
appears, check where you are:
OUTPUT
<node>
853340 8
The host name is now a compute node, not a login node. The Slurm variables from the earlier episodes exist here too.
An interactive session holds its resources until you leave
Nothing is running for you while you read a web page or get a coffee,
but the CPUs are still reserved, and unavailable to others. Ask only for
what you need and a sensible time, and type exit as soon as
you are finished.
Also, do not use set -e in an interactive shell. The
first command that fails would close your session.
Set up the same environment as in a job script, and a work directory on scratch:
BASH
module purge
module load SciPy-bundle/2023.07-gfbf-2023a
export OMP_NUM_THREADS=${SLURM_CPUS_PER_TASK}
WORKDIR=/scratch/SCWFXXXXX/${USER}/interactive/${SLURM_JOB_ID}
mkdir -p "${WORKDIR}" && cd "${WORKDIR}"
cp "${SLURM_SUBMIT_DIR}/sweep.py" .
Challenge: how many CPUs do I see?
In the “Finding the software” episode we ran this one-liner on the login node and got 192 twice:
Run it inside your interactive session. Do the two numbers differ? What does this tell you about where to run your script?
The second number, the CPUs your process is allowed to use, should equal the CPUs you asked for (8). The first can still show all the CPUs of the node. Slurm limits where your processes can run, which is why compute work belongs on the compute nodes.
Why tmux?
If your connection drops, the srun command in your
terminal is killed, and so is your interactive session. We want the
session to survive. The trick is to start tmux
first, on the login node, and run srun
inside it. The srun command then belongs
to tmux, not to your network connection, and carries on
while you are away. tmux also lets us split the window into
panes, to run the program in one and watch it in another.
The full guide to tmux is in the Falcon tmux
documentation page, which you should keep open. Here are the few
keys we need. Every command starts with the prefix,
Ctrl+b: press it, release it, then press the next key.
| Keys | Action |
|---|---|
tmux new -s monitor |
start a new session called monitor
|
Ctrl+b then "
|
split the window: one pane above the other |
Ctrl+b then %
|
split the window: panes side by side |
Ctrl+b then an arrow key |
move to the neighbouring pane |
Ctrl+b then d
|
detach: leave the session running and return to the normal shell |
tmux ls |
list your sessions |
tmux attach -t monitor |
reattach to a session |
Ctrl+b then [
|
scroll back through the output (q to leave) |
Choose a login node
falconlogin.cf.ac.uk alternates between two login nodes
(falconlogin01 and falconlogin02), and a
tmux session lives only on the node where it was started.
Connect to one of them by name, and use the same one when you reattach.
A second entry in your SSH config file, for example
Host falcon1 with
HostName falconlogin01.cf.ac.uk, makes it easy.
Setting up the monitoring window
If you are in the interactive session from before, type
exit to leave it, and log out. We will start again in the
right order.
-
Connect to a login node and start
tmux: In this first pane, start the interactive session and set up the environment, exactly as above.
-
Split the window with
Ctrl+bthen". The new pane is a shell on the login node, not on your compute node, becausetmuxitself runs on the login node. Find your job number:OUTPUT
JOBID PARTITION NAME USER ST TIME NODES NODELIST(REASON) 853340 <partition> interactive c.myuser R 4:12 1 <node> -
Start a second shell inside the same allocation, on the same compute node:
--jobidsays which allocation to join.--overlaplets this new shell share the CPUs already held by the first, which Slurm otherwise may not allow. Check withhostnamethat both panes are on the same compute node.
Your window now has two panes on the same compute node: the program will run in the first, and we watch it from the second.
Challenge: survive a disconnection
- With both panes set up, detach with
Ctrl+bthend. - Close your terminal window completely.
- Open a new one, connect to the same login node, and reattach.
- What is still there?
Run the program and watch it
Our script has two stages: a linear algebra stage that can use several CPUs, followed by a plain Python loop that uses one. A single run is short, so run it several times in a row in the first pane:
Looking with htop
In the second pane:
- The bars at the top show each CPU of the node. A compute node has many CPUs, and most belong to other users’ jobs. Yours are the ones that light up when the program runs.
- In the process list,
pythonhas a CPU% column. A value of 100% means one CPU is fully used. A multi-threaded program can go above 100%: 800% means eight CPUs. - The RES column is the memory the process is using.
It should be close to the
MaxRSSwe saw withsacct. - Press
F10orqto quit.F6chooses the sort column, andF5shows the process tree.
Watch what happens each time sweep.py runs. For a few
seconds many CPUs fill with load and the CPU% of python
rises towards 800%. Then it drops to about 100% and one CPU does
everything for the rest of the run, until the next run starts the cycle
again.
Challenge: spot the switch
Watch htop through a few runs.
- How long does the burst on many CPUs last, compared with the single-CPU part?
- Which part of
sweep.pyis each of them? - How does this match the results of the scaling test?
- The many-CPU burst is short (a few seconds), and the single-CPU part is longer (more than ten seconds).
- The burst is
pca_variance, which uses a multi-threaded linear algebra library. The long single-CPU part ismean_pair_distance, the Python loop. - This is the picture of Amdahl’s law: the serial part dominates the time, which is why more CPUs gave so little.
Looking with ps
htop is easy to read, but ps can be used in
scripts, and shows exactly what each thread is doing. Find the
program:
OUTPUT
PID NLWP %CPU ELAPSED CMD
1124712 8 247 00:12 python sweep.py 1
-
NLWPis the number of threads. Our process tops at 8, when the linear algebra library function starts execution.. -
%CPUinpsis the CPU time since the process started, divided by the time since it started. It is an average over the whole life of the process, not a live reading, so it hides a change like the one we want to see. For that, look at the state of each thread.
Add -L to list one line per thread, with the CPU each
thread last ran on (PSR) and its state
(STAT):
During the linear algebra, many threads are running
(R):
OUTPUT
PID LWP PSR STAT %CPU CMD
1122514 1122514 4 Ss 0.0 /usr/bin/bash
1122761 1122761 2 Ss 0.0 /usr/bin/bash
1126961 1126961 5 S+ 0.0 watch ps -L -u c.myuser -o pid,lwp,psr,stat,pcpu,cmd
1126972 1126972 4 Rl+ 101 python sweep.py 2
1126972 1127139 7 Rl+ 106 python sweep.py 2
1126972 1127140 1 Rl+ 105 python sweep.py 2
1126972 1127141 2 Rl+ 105 python sweep.py 2
1126972 1127142 2 Rl+ 105 python sweep.py 2
1126972 1127143 3 Rl+ 106 python sweep.py 2
1126972 1127144 6 Rl+ 106 python sweep.py 2
1126972 1127145 5 Rl+ 106 python sweep.py 2
1127150 1127150 3 S+ 0.0 watch ps -L -u c.myuser -o pid,lwp,psr,stat,pcpu,cmd
1127151 1127151 0 R+ 0.0 ps -L -u c.myuser -o pid,lwp,psr,stat,pcpu,cmd
...
During the Python loop, only one is, and the others sleep
(S):
OUTPUT
PID LWP PSR STAT %CPU CMD
1122514 1122514 4 Ss 0.0 /usr/bin/bash
1122761 1122761 7 Ss 0.0 /usr/bin/bash
1126282 1126282 7 Rl+ 96.3 python sweep.py 8
1126282 1126290 1 Sl+ 91.1 python sweep.py 8
1126282 1126291 2 Sl+ 91.5 python sweep.py 8
1126282 1126292 5 Sl+ 91.5 python sweep.py 8
1126282 1126293 3 Sl+ 91.7 python sweep.py 8
1126282 1126294 4 Sl+ 91.7 python sweep.py 8
1126282 1126295 6 Sl+ 91.8 python sweep.py 8
1126282 1126296 0 Sl+ 91.7 python sweep.py 8
1126283 1126283 6 S+ 0.0 watch ps -L -u c.myuser -o pid,lwp,psr,stat,pcpu,cmd
1126316 1126316 0 S+ 0.0 watch ps -L -u c.myuser -o pid,lwp,psr,stat,pcpu,cmd
1126317 1126317 6 R+ 0.0 ps -L -u c.myuser -o pid,lwp,psr,stat,pcpu,cmd
...
Busy is not the same as useful
htop and ps show which CPUs are
busy, not whether the work they do is
useful. A thread that spins, waiting for other threads,
looks just like a thread that computes. That is the same limit we met
with seff in the previous episode, and the reason a 128-CPU
run can fill every bar and still be slow. To see what the busy
CPUs are doing we need a profiler, which is the subject of a later
episode.
Watching a batch job
The technique in step 4 works for any running job, not only an
interactive one. If you submitted a batch job and want to look at its
node, find its number with squeue -u $USER, and attach a
shell to it:
You are now on the node where your job runs, and can watch it with the same tools. Please look, and do not change anything: the job is not an interactive session, and anything you start there takes resources from it.
Cleaning up
When you have finished:
- Leave the monitoring shell with
exit. - In the first pane,
exitto end the interactive session and release the CPUs. Check withsqueue -u $USERthat the job is gone, and usescancel <jobid>if it is not. - Close the
tmuxpanes withexit, or end the whole session withtmux kill-session -t monitor.
Quick reference
| Command | What it does |
|---|---|
srun ... --pty bash |
start an interactive session on a compute node |
srun --jobid=<id> --overlap --pty bash |
open another shell in a running job |
htop -u $USER |
live view of CPUs, memory and your processes |
ps -u $USER -o pid,nlwp,pcpu,etime,cmd |
your processes, with thread count |
ps -L -o pid,lwp,psr,stat,pcpu,cmd -p <pid> |
one line per thread, with its state |
watch -n 0.5 '<command>' |
repeat a command every half second |
Key Points
- An interactive session (
srun --pty bash) gives you a shell on a compute node. Exit it when you are done, because it holds its resources. - Start
tmuxon a named login node first and runsruninside it. The session then survives a lost connection. - A second shell in the same allocation
(
srun --jobid=<id> --overlap --pty bash) lets you watch a running job. -
htopshows CPU use live.ps -Lshows what each thread is doing, and the number of threads in stateRis the number of busy CPUs. - The
%CPUinpsis an average over the process’s life, not a live value. - Our script is parallel for a few seconds and serial for the rest, which is what limited the speedup.
- Busy CPUs are not necessarily useful work. A profiler shows what they are doing.