-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobhist
executable file
·83 lines (72 loc) · 2.49 KB
/
jobhist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env bash
# jobhist
# Display a compact history of user's jobs with basic job information
# Default setting is to display job history for the past 7 days
# Based on output from sacct
set -euo pipefail
# ANSI color codes
RED='\033[0;31m'
BLUE='\033[0;34m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
PURPLE='\033[0;35m'
RESET='\033[0m'
# Set default values
DAYS=7
user=$USER
# Define options
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
printf "%s\t%s\n" "Usage: $(basename "$0")" "[OPTIONS]"
printf "\t%s\n" "-h, --help"
printf "\t%s\t%s\n" "-d, --days" "number of days to list jobs from"
printf "\t%s\t%s\n" "-u, --user" "user whose jobs to list"
exit 0
;;
-d | --days)
if [[ $2 =~ ^[0-9]+$ && $2 -ge 1 ]]; then
DAYS=$2
echo "Thanks for specifying $DAYS days"
else
printf "${RED}%s%s\n" "ERROR: " "days must be a positive integer"
exit 1
fi
shift 2
;;
-u | --user)
if [[ $2 =~ ^[[:alnum:]]+$ ]]; then
user=$2
else
printf "${RED}%s%s\n" "ERROR: " "user must be an alphanumeric string"
exit 1
fi
shift 2
;;
-* | --*=) # unsupported flags
echo -e "${RED}Error: Unsupported flag $1${RESET}" >&2
# rerun the script with --help
"$0" --help
exit 1
;;
*) # preserve positional arguments
PARAMS+=("$1")
shift
;;
esac
done
# Check if user exists
if ! id -u "$user" >/dev/null 2>&1; then
printf "%s\n" "No such user: $user"
exit 1
fi
# Set sacct format
format="Start,jobid%13,jobname%12,partition,state,elapsed%11,nnodes%5,ncpus%4,reqmem%8,reqtres%-50"
printf "%s\n" "---------------------------------------------------------------------------------------------------------"
printf "%s\n" " Startdate Job ID Job Name Partition State Elapsed Nodes CPUs Memory GPUs"
printf "%s\n" "------------------- ------------- ------------ ---------- ---------- ----------- ----- ---- -------- ----"
# Calculate the start date
start_date=$(date --date="-${DAYS} day" +%Y-%m-%d)
# Run sacct command and extract number of GPUs requested
sacct --noheader -X -o "$format" -u "$user" -S "$start_date" |
sed -r 's/billing=.*gres\/gpu=([^,]*),.*/\1/; s/billing=.*/0/'