-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjupyter_cluster.sh
197 lines (175 loc) · 6.29 KB
/
jupyter_cluster.sh
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/bash
#1. Defining default variables
u="moicoll"
c=1234
l=1234
s="/home/moicoll/jupyter_cluster/job.sh"
w=10
k="empty"
error="empty"
out="empty"
#2. help function
help()
{
echo "Usage: bash $0 [-u,-c,-l,-w,-s,-h]
Command Explanation Default
------- ----------- -------
-u, --user <string> GenomDK user ${u}
-c, --clusterport <int> cluster port ${c}
-l, --localport <int> local port ${l}
-w, --waitingtime <int> waiting time for job status ${w}
checking and shh tunnel connection
-s, --jobpath <string> path to the job to be submitted to ${s}
slurm
-h, --help prints this help message
"
exit 1
}
#3. parssing variables
while getopts "u:e:c:l:m:n:s:krh:" o; do
case "${o}" in
u)
u=${OPTARG}
;;
c)
c=${OPTARG}
;;
l)
l=${OPTARG}
;;
w)
w=${OPTARG}
;;
s)
s=${OPTARG}
;;
h)
help
;;
*)
help
;;
esac
done
shift $((OPTIND-1))
#1) Checking ports
printf "\n"
printf "1) Checking if the port ${l} is free to use\n"
echo "-------------------------------------------"
#https://askubuntu.com/questions/447820/ssh-l-error-bind-address-already-in-use
p=`lsof -ti:${l} | wc -l`
if [ ${p} -gt 0 ];
then
printf " There are processes that are running on port ${l}. This script tries to open a ssh tunnel from this laptop to the cluster using this port.\n"
printf " You will need to kill them before proceding a command like this:\n"
printf "\n $ lsof -ti:${l} | xargs kill -9\n\n"
while [ ! -z ${k} ] && [ ${k} != "Y" ] && [ ${k} != "n" ];
do
printf " Do you want this script to kill the process? [Y]/n: "
read k
done;
if [ -z ${k} ] || [ ${k} == "Y" ];
then
printf " Killing the port!\n"
lsof -ti:${l} | xargs kill -9
else
printf " Good bye then!\n"
exit
fi
else
printf " No processes running on port ${l}\n"
fi
#2) Checking old scripts
printf "\n"
printf "2) Checking if there is already a jupyter cluster job\n"
echo "-----------------------------------------------------"
jobname=`ssh ${u}@login.genome.au.dk 'egrep "#SBATCH -J" '${s}' | awk '\''{print $3}'\'''`
p=`ssh ${u}@login.genome.au.dk 'squeue -n '${jobname}' -u '${u}' -h | wc -l'`
if [ ${p} -gt 0 ];
then
printf " Found ${p} jobs with the same name ${jobname}...\n"
ssh ${u}@login.genome.au.dk 'for job in `squeue -n '${jobname}' -u '${u}' -h | awk '\''{print $1}'\''`; do echo " - Canceling job" ${job}; scancel ${job}; done';
else
printf " No old jobs have been found\n"
fi
#3) Submitting the job to the slurm queue
printf "\n"
printf "3) Submitting the job to the slurm queue\n"
echo "----------------------------------------"
o=`ssh ${u}@login.genome.au.dk sbatch ${s}`
j=`echo ${o} | awk '{print $4}'`
echo " Submitting the job ${s} with jobid: " ${j};
#Check if the job is in the slurm system queue (${p}) and has been allocated to any node (${n})
# ${n} : node id where the job is alocated when running
# ${p} : variable that denotes if the job is still on the slurm queue system (either waiting or running) as 1 and cancelled if 0
n=`ssh ${u}@login.genome.au.dk squeue --noheader | awk '{if($1=='${j}' && $5 == "R"){print $8}}'`
p=`ssh ${u}@login.genome.au.dk squeue --noheader | awk '{if($1=='${j}'){print}}' | wc -l`
while [ "${n}" == "" ] && [ ${p} -eq 1 ];
do
for x in `seq ${w} -1 0`; do printf " No node has been assigned yet. Waiting ${w} seconds before checking... %2d\r" ${x}; sleep 1; done;
printf "\n Checking again...\n"
n=`ssh ${u}@login.genome.au.dk squeue --noheader | awk '{if($1=='${j}' && $5 == "R"){print $8}}'`
p=`ssh ${u}@login.genome.au.dk squeue --noheader | awk '{if($1=='${j}'){print}}' | wc -l`
done
#Check if the job has been cancelled
if [ ${p} -eq 0 ];
then
echo " Job has failed"
#Let the user check the standard output
while [ ! -z ${out} ] && [ ${out} != "Y" ] && [ ${out} != "n" ];
do
printf " Do you want to see the standard output? [Y]/n: "
read out
done;
if [ -z ${out} ] || [ ${out} == "Y" ];
then
outfile=`ssh ${u}@login.genome.au.dk 'egrep "#SBATCH -e" '${s}' | awk '\''{print $3}'\'''`
echo ""
echo "--------------------------- OUT ---------------------------"
ssh ${u}@login.genome.au.dk 'cat '${errorfile}''
echo "-----------------------------------------------------------"
echo ""
fi
#Let the user check the standard error
while [ ! -z ${error} ] && [ ${error} != "Y" ] && [ ${error} != "n" ];
do
printf " Do you want to see the standard error? [Y]/n: "
read error
done;
if [ -z ${error} ] || [ ${error} == "Y" ];
then
errorfile=`ssh ${u}@login.genome.au.dk 'egrep "#SBATCH -e" '${s}' | awk '\''{print $3}'\'''`
echo ""
echo "-------------------------- ERROR --------------------------"
ssh ${u}@login.genome.au.dk 'cat '${errorfile}''
echo "-----------------------------------------------------------"
echo ""
fi
#If the job is running...
else
echo " Job is running in node " ${n};
#4) Open a shh tunnel to the cluster and open the browser
printf "\n"
printf "4) Open a shh tunnel to the cluster and open the browser\n"
echo "--------------------------------------------------------"
echo " Oppening the ssh tunnel to the cluster";
ssh -L ${l}:${n}:${c} ${u}@login.genome.au.dk -N &
for x in `seq ${w} -1 0`; do printf " Waiting ${w} seconds before connecting... %2d\r" ${x}; sleep 1; done;
printf "\n Connecting to cluster and oppening the browser\n"
open --new -a "Google Chrome" --args "https://localhost:${l}/"
out="empty"
while [ ! -z ${out} ] && [ ${out} != "Y" ] && [ ${out} != "n" ];
do
printf " Do you want to see the standard output? [Y]/n: "
read out
done;
if [ -z ${out} ] || [ ${out} == "Y" ];
then
errorfile=`ssh ${u}@login.genome.au.dk 'egrep "#SBATCH -e" '${s}' | awk '\''{print $3}'\'''`
echo ""
echo "--------------------------- OUT ---------------------------"
ssh ${u}@login.genome.au.dk 'tail -f '${errorfile}''
echo "-----------------------------------------------------------"
echo ""
fi
fi