Search code examples
pythonhpcslurmsnakemake

How to check if my code runs inside a SLURM environment?


I am working on a workflow using Snakemake that is supposed to be portable to any Linux based system, but is mainly developed to run on a hpc using SLURM. For optimization when using SLURM I'd like to check if code runs in a SLURM environment and then alter tasks slightly to improve on resources management.

My first idea was to just try and resolve the environment variable $SLURM_JOB_ID via os.path.expandvars, but that is kinda dirty in my opinion, so is there a clean way to just check the environment?


Solution

  • Checking for the environment variable is the way to go. In Python you would do it like this:

    import os
    if "SLURM_JOB_ID" in os.environ:
        print("Running in Slurm")
    else:
        print("NOT running in Slurm")