To start an attachable interactive Slurm job in the background, you typically use the srun or salloc command with the --pty and --detach options, or you can use screen or tmux to manage your session. Here’s how you can do it:
srun with --pty and --detachThis method allows you to start an interactive job and detach from it, so you can reattach later.
bashsrun --pty --job-name=myjob --time=01:00:00 --mem=4G --nodes=1 --ntasks-per-node=1 --detach bash
--pty: Allocates a pseudo-terminal.--detach: Detaches the job and runs it in the background.--job-name=myjob: Gives your job a name (optional but helpful).--time=01:00:00: Sets the maximum runtime.--mem=4G: Requests 4GB of memory.--nodes=1: Requests 1 node.--ntasks-per-node=1: Requests 1 task per node.To reattach:
Find your job ID with squeue -u $USER, then reattach using:
bashsattach <jobid>
salloc with screen or tmuxIf your cluster doesn’t support --detach with srun, you can use salloc to allocate resources and then start a screen or tmux session inside it.
bashsalloc --job-name=myjob --time=01:00:00 --mem=4G --nodes=1 --ntasks-per-node=1
This will give you a shell on a compute node.
screen or tmux sessionbashscreen -S mysession # or tmux new -s mysession
Now, run your interactive commands inside this session.
screen: Press Ctrl+A then D.tmux: Press Ctrl+B then D.bashscreen -r mysession # or tmux attach -t mysession
sbatch with an interactive scriptIf you want to submit an interactive job as a batch script and reattach later, you can use a script like this:
interactive.sh
bash#!/bin/bash #SBATCH --job-name=myjob #SBATCH --time=01:00:00 #SBATCH --mem=4G #SBATCH --nodes=1 #SBATCH --ntasks-per-node=1 screen -S mysession
Submit it with:
bashsbatch interactive.sh
Then, reattach with:
bashscreen -r mysession
squeue -u $USER and use sattach or screen -r/tmux attach as needed.