Jump to content

Tutorials/Slurm-Interactive-Sessions: Difference between revisions

From HPCwiki
Created page with "= Interactive Sessions: sinteractive, srun and salloc = ''Part 1 of the Slurm for beginners series.'' In this tutorial you will get your first piece of a compute node and type commands on it, just like on your own computer. You will learn three commands: * <code>sinteractive</code> — the easy way: open a shell on a compute node. * <code>srun</code> — run one command on a compute node. * <code>salloc</code> — reserve resources..."
 
m Honfi001 moved page Tutorials/slurm-interactive-sessions to Tutorials/Slurm-Interactive-Sessions without leaving a redirect
 
(No difference)

Latest revision as of 10:29, 23 November 2026

Interactive Sessions: sinteractive, srun and salloc

Part 1 of the Slurm for beginners series.

In this tutorial you will get your first piece of a compute node and type commands on it, just like on your own computer. You will learn three commands:

  • sinteractive — the easy way: open a shell on a compute node.
  • srun — run one command on a compute node.
  • salloc — reserve resources first, then use them.

You need: an Anunna account, and the ability to log in and type basic Linux commands. If you are unsure about the second part, try the Linux self-assessment first.

Time: about 30 minutes.

Before you start: login nodes and compute nodes

When you log in to Anunna you land on a login node. Think of it as the reception desk of a big laboratory. Everyone walks in through it, so it is a good place to look around, edit files and organise your work. It is not the place to run experiments. If you do heavy work there, everyone queueing behind you notices.

The real work happens in the lab itself: hundreds of compute nodes. You cannot just walk into one. You ask Slurm, the cluster's scheduler, and Slurm finds you a free spot and gives you a key. Slurm is the lab manager who books the benches.

There are two ways to ask Slurm for a bench:

Interactive session Batch job
What it is You get a shell on a compute node and type commands yourself You write a script and Slurm runs it for you, unattended
Good for Trying things out, testing, installing software, debugging Real analyses, long runs, many runs
You must stay connected? Yes — close your laptop and the session ends No — log out and it keeps running
Covered in This tutorial The next one

Step 1: Your first session with sinteractive

Type this on the login node:

sinteractive

You will see something like:

srun: job 19271078 queued and waiting for resources
srun: job 19271078 has been allocated resources

and then a new prompt. That number is your job ID: every request to Slurm gets one. Nothing else looks different, so check where you are:

hostname

Instead of a login node you will see the name of a compute node, for example node253. You are now on a compute node. Everything you type runs there.

When you are done, leave:

exit

Run hostname again and you are back on the login node. Leaving gives your bench back to Slurm so someone else can use it. Always exit when you are finished.

Step 2: Ask for what you need

Typing just sinteractive gives you a tiny default bench: one CPU core, very little memory and a short time limit. Fine for a peek, too small for real work. You tell Slurm what you need with three options:

Option Means Example
-c How many CPU cores -c 4 = 4 cores
--mem How much memory (RAM) --mem 8G = 8 gigabytes
--time How long, in minutes --time 120 = 2 hours

So a session with 2 cores, 4 GB of memory and one hour looks like this:

sinteractive -c 2 --mem 4G --time 60

Once you are in, check that you got what you asked for:

nproc

This prints 2: the number of cores that are yours. (Some other tools, like Python's os.cpu_count(), report every core on the whole machine instead. nproc tells the truth about what you were given.)

How much should I ask for? Start small. Small requests start quickly, and big requests wait longer in the queue. Slurm also holds you to your request: a program that uses more memory than you asked for can be stopped, and when your time runs out the session ends, even mid-command. Ask for a bit more than you think you need, not ten times more.

If you see... It means... Try...
queued and waiting for resources for a while The cluster is busy, or your request is big Waiting a little, or asking for less
The command gives up after about a minute sinteractive waits at most 60 seconds for a free spot Asking for less, or trying again later
Your session suddenly closes Your time ran out, or your connection dropped A longer --time; a batch job for long work

Step 3: srun — run one command on a compute node

Here is a secret: sinteractive is a small shortcut around another command, srun. Underneath, it runs:

srun -c 2 --mem 4G --time 60 -I60 -N 1 -n 1 --pty bash -i

That means "start bash on a compute node and connect my keyboard to it". You don't need to remember this. It is just to show that srun is the real worker.

srun can also run a single command on a compute node and bring you straight back:

srun -c 1 --mem 1G --time 5 hostname

Slurm finds a spot, runs hostname there, prints the compute node's name, and you are back on the login node, all in one go. The options are the same -c, --mem and --time you already know.

Use this when you want to run one thing without opening a whole session. Your terminal stays busy until the command finishes, so it suits short tasks.

Step 4: salloc — book now, connect when you like

With sinteractive, the bench is yours only while you are sitting at it. Close the session and it is gone. salloc works more like booking a lab bench for the afternoon: the booking is yours until the time runs out, and you can walk away and come back as often as you like.

It takes two commands: one to book, and one to connect.

Book

salloc --no-shell -c 2 --mem 4G --time 120

The options are the same ones you know from sinteractive. --no-shell means "just make the booking, don't open anything yet". Once Slurm has found you a spot, you see:

salloc: Granted job allocation 12345678

and you are back at your normal prompt on the login node. Nothing is running yet, but those 2 cores and 4 GB are now reserved for you. Write down the job ID (here 12345678). If you forget it, this shows all your jobs:

squeue -u $USER

Connect

srun --jobid=12345678 --overlap --pty bash

Run hostname and you will see you are on a compute node, inside your booking. The three options mean:

Option Means
--jobid=12345678 Use this booking (put in your own job ID)
--overlap Share the booking with anything else already running in it, so you can connect more than once
--pty bash Give me a shell I can type in

You don't need -c, --mem or --time here: you get what you booked.

Leave, and come back

Type exit and you are back on the login node. Your booking is still there. Run the same srun command again to reconnect, after lunch, or from a second terminal at the same time.

If your connection drops, the same rule applies: the booking survives. But anything that was running in your shell is stopped, so you come back to a fresh shell. For a program that must keep running while you are away, use a batch job (the next tutorial).

Finish

When you are done, cancel the booking:

scancel 12345678

Don't skip this. exit only closes your shell. The booking stays until you cancel it or its time runs out, and until then those cores and that memory are held in your name, unused, while others wait in the queue. Run squeue -u $USER afterwards. If the job is gone, you are done.

Which one should I use?

I want to... Use Example
Work on a compute node as if it were my own computer sinteractive sinteractive -c 2 --mem 4G --time 60
Run one quick command on a compute node srun srun -c 1 --mem 1G --time 5 hostname
Book resources for a few hours and connect to them whenever I like salloc --no-shell, then srun --jobid salloc --no-shell -c 2 --mem 4G --time 120, then srun --jobid=12345678 --overlap --pty bash

If in doubt, use sinteractive. It covers most everyday needs, and it is hard to get wrong.

Try it yourself

  1. Open a session with 1 core, 2 GB of memory and 30 minutes. Check which node you are on and how many cores you have. Then exit.
  2. Without opening a session, use srun to run date on a compute node.
  3. Use salloc to book 1 core and 1 GB for 30 minutes. Connect to it, run hostname, and exit. Connect again and check you are on the same node. Then cancel the booking and check that it is gone.

Answers:

  1. sinteractive -c 1 --mem 2G --time 30, then hostname and nproc (which prints 1), then exit.
  2. srun -c 1 --mem 1G --time 5 date
  3. salloc --no-shell -c 1 --mem 1G --time 30 prints your job ID. Then run srun --jobid=<your job ID> --overlap --pty bash, hostname and exit, and repeat. hostname shows the same node both times. Finish with scancel <your job ID>, and squeue -u $USER no longer lists it.

Cheat sheet

Command What it does
sinteractive -c 2 --mem 4G --time 60 Open a shell on a compute node
srun -c 1 --mem 1G --time 5 my_command Run one command on a compute node
salloc --no-shell -c 2 --mem 4G --time 120 Book resources and get a job ID
srun --jobid=<ID> --overlap --pty bash Connect to your booking (as often as you like)
scancel <ID> Cancel a booking (or any job)
hostname Which machine am I on?
nproc How many cores do I have?
squeue -u $USER What jobs do I have right now?
exit Leave the session (with sinteractive, this also gives the resources back)

Next

Interactive sessions are for trying things out. When you have something that works and want it to run on its own, even after you log out, you need a batch job. That is the next tutorial in this series.

Want to go deeper into interactive work: cores versus tasks, several nodes, and keeping an allocation open? The Interactive Slurm series starts with Part 1: srun.

See also