ThatsRightJack
ThatsRightJack

Reputation: 751

Parse SLURM job wall time to bash variables

With SLURM, I run the command

squeue -u enter_username

and I get an table output with the following heading

JOBID PARTITION     NAME     USER ST       TIME  NODES NODELIST(REASON)

I'm trying to capture the duration of time the job has been running for. I couldn't find an environmental variable provided by SLURM to capture this time, so I think I'm left parsing the output of squeue. This is not as easy as I thought it would be, because the wall clock does not have a fixed format. In other words, it doesn't always show dd-HH:MM:SS. If there are no days, then the output is just HH:MM:SS, and if there are no hours the output is MM:SS, etc.

I'm doing this with bash and I need to capture the day (dd) and hour (HH) and assign each of them to a variable. I'm having a hard time doing this when the format is dynamic.

To capture the time entry, I'm simply doing the following (within a SLURM bash script)

 time_str=$(squeue -u enter_username | grep "$SLURM_JOB_ID" | cut -d "R" -f2- | gawk '{print $1}')

As I said before, time_str does not have a fixed format. Hoping someone with experience can help.

Upvotes: 0

Views: 834

Answers (2)

ThatsRightJack
ThatsRightJack

Reputation: 751

The solution presented by @Andrew Vickers above works as expected. However, I took this one step further to enforce a fixed 2 digit format

index=0
while [ ${index} -lt 4 ]; do
    if [ ${#parts[${index}]} -lt 2 ]; then
        parts[${index}]="0${parts[${index}]}"
    fi
((index++))
done

The conditional check could be incorporated into his answer, but his loop would need to be adjusted to format all variables.

Upvotes: 0

Andrew Vickers
Andrew Vickers

Reputation: 2654

From reading the man page of the squeue command, it seems that you can simplify the problem by having squeue only output the information you need:

squeue -h -j ${jobid} -O timeused 

Then your task is simply to parse that output, which can be done as follows:

#!/bin/bash

line="-$(squeue -h -j ${jobid} -O timeused)" # Leading '-' aids parsing.

parts=( 0 0 0 0 )
index=3
while [ ${#line} -gt 0 ]; do
  parts[${index}]=${line##*[-:]}
  line=${line%[-:]*}
  ((index--))
done

Now the array ${parts[*]} contains exactly 4 elements, 0 to 3, representing days, hours, minutes and seconds respectively.

Upvotes: 1

Related Questions