-
Notifications
You must be signed in to change notification settings - Fork 7
/
smux
executable file
·65 lines (56 loc) · 1.64 KB
/
smux
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
#!/usr/bin/env bash
# shellcheck disable=SC2128
###############################################################################
# Launch a tmux server on a slurm node and interact with it from a login node #
# (particularly useful when a slurm cluster has multiple login nodes). Has #
# the same command line interface as tmux with an additional 'batch' command #
# which starts the tmux server using sbatch. Options are forwarded to the #
# underlying sbatch command. #
###############################################################################
set -o errexit -o nounset -o pipefail
SBATCH_JOB_NAME="$(basename "$BASH_SOURCE")"
STATE_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/$SBATCH_JOB_NAME"
JOB_ID_FILE="$STATE_DIR/jobid"
mkdir -p "$STATE_DIR"
touch "$JOB_ID_FILE"
export SBATCH_JOB_NAME
function job_id(){
cat "$JOB_ID_FILE"
}
function job_state(){
[[ -n $(squeue -ht "$1" -j "$(job_id)" 2>/dev/null) ]]
}
function cancel(){
if job_state running,pending; then
scancel "$(job_id)"
fi
truncate -s 0 "$JOB_ID_FILE"
}
function multiplex(){
while job_state pending; do
sleep 1
done
if job_state running; then
srun --overlap --pty --jobid "$(job_id)" tmux "$@"
else
echo "No server running on any slurm node!" \
"Run '$SBATCH_JOB_NAME batch [options]' first." >&2
exit 1
fi
}
if [[ "$BASH_SOURCE" == "$0" ]]; then
case "${1:-}" in
batch)
sbatch --parsable "${@:2}" --wrap 'tmux -D' >"$JOB_ID_FILE"
;;
switch)
echo "${2:-$(job_id)}" >"$JOB_ID_FILE"
;;
cancel)
cancel
;;
*)
multiplex "$@"
;;
esac
fi