Simple tool to wrap and execute sbatch commands from python scripts or Jupyter Notebooks with conda envs and config files. Less mess, more reproducibility and security (if published to GitHub, for instance).
Based on jbatch.
- ⚙️ Config Files: Create config files in
YAMLformat and reuse them. Make adefault.yamlin~/.config/slurmbot/default.yaml - 🐍 Python Variables: Easily integrate Python variables into your Slurm job commands.
- 🛠️ Conda: Activate Conda environments with the
condaproperty and use conda_prefix like "conda activate &&" in the config yaml – see example config file. Also can use prefix to run smth before executing each job. - 📱 Telegram (teleslurm): Optional notifications to Telegram when jobs start/finish. Uses
BOT_TOKEN,CHAT_ID,THREAD(and per-chat keys likeBOT_TOKEN_chat1,CHAT_ID_chat1,THREAD_chat1) in the config.
pip install slurmbot
from slurmbot.slurmbot import SlurmBot
sb = SlurmBot() # using default yaml from ~/.config/slurmbot/default.yaml, if exists OR specify path to config
samples = ["SRR000001", "SRR000002"]
for sample in samples:
sb.run(f"fasterq-dump {sample}", conda="ngs", mem=4, cpus=4, dry=True, v=2, time=48) # 🚨 time in hours!With Telegram (teleslurm) — message "started" / "finished" to a chat:
from slurmbot.slurmbot import SlurmBot
sb = SlurmBot()
sb.run(f"ls -la", dry=False, mem=4, cpus=4, v=2, time=48, teleslurm=True) # will sent messages upon the start, finish and errorsCLI:
python -m slurmbot.teleslurm -s -c chat1 "MESSAGE" # 🚨 do not forget to specify chat1 settings in config if use -cMinimal example: default.yaml. Put your config in ~/.config/slurmbot/default.yaml.
- SlurmBot(config_path=None): Create a bot bound to a YAML config file. If
config_pathis omitted, SlurmBot tries to use~/.config/slurmbot/default.yamlwhen it exists. - Config keys (YAML, also used as defaults for
sb.run):- account / partition / reservation: Passed through to
sbatch(--account,--partition,--reservation). - cpus / mem / time: Default CPU cores, memory (GB), and walltime (hours) for jobs.
- logdir: Directory for
sbatchlogs (logdir/%j.out,logdir/%j.err). - name: Default Slurm job name (falls back to the first token of
cmdif empty). - prefix: Shell snippet run before each job (e.g.
unset SLURM_EXPORT_ENV). - conda_prefix / conda: Activate a Conda env before running the command.
- account / partition / reservation: Passed through to
sb.run(cmd, **kwargs)main options:- cmd: Shell command to execute via
sbatch(wrapped in/bin/bash -c). - cpus, mem, time, logdir, account, partition, reservation, name: Override the corresponding config keys per-call.
- dependency: Slurm dependency; can be a single job id or a list, turned into
--dependency=afterok:<ids>. - dry: If
True, print thesbatchcommand without submitting. - v: Verbosity;
v > 1prints the fullsbatchcommand before submitting. - conda / prefix: Per-call overrides for environment/command prefix (merged with config values).
- teleslurm: If
True, send Telegram messages when the job starts, finishes, and on errors. - teleslurm_status: If
True, attach CPU/memory and Slurm queue/CPU/GPU stats to Telegram messages. - teleslurm_chat: Optional chat key (e.g.
"Chat1"), usingBOT_TOKEN_Chat1,CHAT_ID_Chat1,THREAD_Chat1from config. - Any other
kwargsmatching config keys are merged into the config for that call and passed through tosbatch.
- cmd: Shell command to execute via
- Adding history with job_ids
- Adding dependencies