Slurm – HTTP REST API

The Slurm Rest API allows users to interact with Slurm from a standard HTTP REST API.

Request a token on the login node (default 1 hour lifespan):

$ scontrol token
SLURM_JWT=eyJhbGciOiJI...................

This command will add token as environment variable:

export $(scontrol token)

Simple test to send "ping" to the main Slurm server:

$ curl -s -k -H X-SLURM-USER-TOKEN:$SLURM_JWT -X GET 'http://idun-slurm:6820/slurm/v0.0.41/ping'
{
  "pings": [
    {
      "hostname": "idun-slurm",
      "pinged": "UP",
      "latency": 613,
      "mode": "primary"
    }
. . . . .

Submit a new job with python.

Set environment variable to be used in the python script:

export SLURM_URL=http://idun-slurm:6820
export API_VER=v0.0.41
export ACCOUNT=support
export $(scontrol token)

Python script to create jobs with command "sleep 60":

$ cat slurm_rest_submit.py
import requests
import json
import os

SLURM_URL = os.environ["SLURM_URL"]
API_VER = os.environ["API_VER"]
USER = os.environ["USER"]
SLURM_JWT = os.environ["SLURM_JWT"]
ACCOUNT = os.environ["ACCOUNT"]
PWD = os.environ["PWD"]

response = requests.post(
    f'{SLURM_URL}/slurm/{API_VER}/job/submit',
    headers={
        'X-SLURM-USER-NAME': f'{USER}',
        'X-SLURM-USER-TOKEN': f'{SLURM_JWT}'
    },
    json={
        "script": "#!/bin/bash\nsleep 60",
        'job': {
            'current_working_directory': f'{PWD}',
            'account': f'{ACCOUNT}',
            "environment": {
            "USER": '{USER}'
        }
        }
    })

try:
    response.raise_for_status()
except requests.exceptions.HTTPError as error:
    print(error.response.text)

print(json.dumps(response.json(), indent=2))

Submit job:

$ python slurm_rest_submit22222.py
{
  "result": {
    "job_id": 20626430,
    "step_id": "batch",
    "error_code": 0,
    "error": "No error",
. . . . . . .

Useful links

https://slurm.schedmd.com/rest_api.html

Scroll to Top