/
script_wrapper.erb.sh
62 lines (52 loc) · 1.78 KB
/
script_wrapper.erb.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
#!/bin/bash
hostname
# Put the script into a temp file on localhost
<% if debug %>
singularity_tmp_file=$(mktemp -p "$HOME" --suffix '_sing')
tmux_tmp_file=$(mktemp -p "$HOME" --suffix "_tmux")
<% else %>
singularity_tmp_file=$(mktemp)
tmux_tmp_file=$(mktemp)
<% end %>
# Create an executable to run in a tmux session
cat << TMUX_LAUNCHER > "$tmux_tmp_file"
#!/bin/bash
<% if email_on_terminated %>
exit_script() {
<%# DO NOT INDENT email_on_terminated may have HEREDOCS %>
<%= email_on_terminated %>
trap - SIGINT SIGTERM # clear the trap
kill -- -$$ # Sends SIGTERM to child/sub processes
}
trap exit_script SIGINT SIGTERM
<% end %>
<%= cd_to_workdir %>
<%= environment %>
<%= email_on_start %>
# Redirect stdout and stderr to separate files for all commands run within the curly braces
# https://unix.stackexchange.com/a/6431/204548
# Swap sterr and stdout after stdout has been redirected
# https://unix.stackexchange.com/a/61932/204548
OUTPUT_PATH=<%= output_path %>
ERROR_PATH=<%= error_path %>
({
timeout <%= script_timeout %>s <%= singularity_bin %> exec --pid <%= singularity_image %> /bin/bash --login $singularity_tmp_file <%= arguments %>
} | tee "\$OUTPUT_PATH") 3>&1 1>&2 2>&3 | tee "\$ERROR_PATH"
<%= email_on_terminated %>
# Exit the tmux session when we are complete
exit 0
TMUX_LAUNCHER
# Create an executable for Singularity to run
cat << SINGULARITY_LAUNCHER > "$singularity_tmp_file"
<%= script_content %>
SINGULARITY_LAUNCHER
# Run the script inside a tmux session
chmod +x "$singularity_tmp_file"
chmod +x "$tmux_tmp_file"
<%= tmux_bin %> new-session -d -s "<%= session_name %>" "$tmux_tmp_file"
# Remove the file
<% if ! debug %>
# Wait 1 second to ensure that tmux session has started before the file is removed
sleep 1
rm -f "$tmux_tmp_file"; rm -f "$singularity_tmp_file"
<% end %>