|
1 | | -#!/usr/bin/env bash |
| 1 | +#!/bin/bash |
| 2 | +###################################################################### |
| 3 | +# A batchtools launch script template for SGE |
| 4 | +# |
| 5 | +# Author: Henrik Bengtsson |
| 6 | +###################################################################### |
| 7 | +## Shell |
| 8 | +#$ -S /bin/bash |
2 | 9 |
|
3 | | -## Job name: |
4 | | -#$ -N fbt-<%= job.hash %> |
| 10 | +## Job name |
| 11 | +#$ -N <%= job.name %> |
5 | 12 |
|
6 | | -## Merge standard error and output: |
7 | | -#$ -j y |
8 | | - |
9 | | -## Direct streams to logfile: |
| 13 | +## Direct streams to logfile |
10 | 14 | #$ -o <%= log.file %> |
11 | 15 |
|
12 | | -## Email on abort (a) and termination (e), but not when starting (b) |
13 | | -#PBS -m ae |
| 16 | +## Merge standard error and output |
| 17 | +#$ -j y |
14 | 18 |
|
15 | 19 | ## Tell the queue system to use the current directory |
16 | 20 | ## as the working directory |
17 | | -## -cwd |
18 | | - |
19 | | -## Mirror environment variables |
20 | | -#$ -V |
| 21 | +#$ -cwd |
21 | 22 |
|
22 | 23 | ## Resources needed: |
23 | | -<% if (exists("resources", mode = "list") && length(resources) > 0) { |
24 | | - if (isTRUE(getOption("future.debug"))) { |
25 | | - R.utils::mcat("resources:") |
26 | | - R.utils::mstr(resources) |
27 | | - } |
28 | | - cat(sprintf("#$ %s\n", resources[["custom"]])) |
| 24 | +<% |
| 25 | + ## As-is resource specifications |
| 26 | + job_declarations <- resources[["asis"]] |
| 27 | + resources[["asis"]] <- NULL |
| 28 | + |
| 29 | + ## Should scheduler "details" be seen? |
| 30 | + details <- isTRUE(resources[["details"]]) |
| 31 | + resources[["details"]] <- NULL |
| 32 | + |
| 33 | + ## Shell "startup" code to evaluate |
| 34 | + startup <- resources[["startup"]] |
| 35 | + resources[["startup"]] <- NULL |
| 36 | + |
| 37 | + ## Shell "shutdown" code to evaluate |
| 38 | + shutdown <- resources[["shutdown"]] |
| 39 | + resources[["shutdown"]] <- NULL |
| 40 | + |
| 41 | + ## Environment modules specifications |
| 42 | + modules <- resources[["modules"]] |
| 43 | + resources[["modules"]] <- NULL |
| 44 | + |
| 45 | + ## SPECIAL: For R CMD check package testing on HPC environments, which |
| 46 | + ## typically uses a temporary working directory that is local, we force |
| 47 | + ## it to use HPC-wide working directory |
| 48 | + path <- tools::R_user_dir("future.batchtools", "cache") |
| 49 | + path <- file.path(path, "R_CMD_check") |
| 50 | + if (!utils::file_test("-d", path)) dir.create(path, recursive = TRUE) |
| 51 | + job_declarations <- c(job_declarations, sprintf("-d %s", path)) |
| 52 | + |
| 53 | + ## SPECIAL: Since we change working directory, the 'startup.Rs' file used |
| 54 | + ## by R CMD check is no longer found |
| 55 | + startup <- c(startup, "export R_TESTS=") |
| 56 | + |
| 57 | + ## Remaining resources are assumed to be of type '-l <key>=<value>' |
| 58 | + opts <- unlist(resources, use.names = TRUE) |
| 59 | + opts <- sprintf("-l %s=%s", names(opts), opts) |
| 60 | + job_declarations <- sprintf("#$ %s", c(job_declarations, opts)) |
| 61 | + writeLines(job_declarations) |
| 62 | +%> |
| 63 | + |
| 64 | +## Bash settings |
| 65 | +set -e # exit on error |
| 66 | +set -u # error on unset variables |
| 67 | +set -o pipefail # fail a pipeline if any command fails |
| 68 | +trap 'echo "ERROR: future.batchtools job script failed on line $LINENO" >&2; exit 1' ERR |
| 69 | + |
| 70 | +echo "Batchtools information:" |
| 71 | +echo "- job name: '<%= job.name %>'" |
| 72 | +echo "- job log file: '<%= log.file %>'" |
| 73 | +echo |
| 74 | + |
| 75 | +<% if (length(startup) > 0) { |
| 76 | + writeLines(startup) |
| 77 | +} %> |
| 78 | + |
| 79 | +echo "Load environment modules:" |
| 80 | +<% if (length(modules) > 0) { |
| 81 | + writeLines(c(sprintf("module load %s", modules), "module list")) |
29 | 82 | } %> |
30 | 83 |
|
31 | | -## UCSF HPC Pilot specific |
32 | | -#$ -S /bin/bash # Required |
33 | | -#$ -l mem_free=1G # Memory usage, required. Note that this is per slot |
34 | | -#$ -R yes # SGE host reservation, highly recommended |
| 84 | +echo "Session information:" |
| 85 | +echo "- timestamp: $(date)" |
| 86 | +echo "- hostname: $(hostname)" |
| 87 | +echo "- Rscript path: $(which Rscript)" |
| 88 | +echo "- Rscript version: $(Rscript --version)" |
| 89 | +echo "- Rscript library paths: $(Rscript -e "cat(shQuote(.libPaths()), sep = ' ')")" |
| 90 | +echo |
35 | 91 |
|
36 | | -## SPECIAL: For R CMD check package testing on HPC environments, which |
37 | | -## typically uses a temporary working directory that is local, we force |
38 | | -## it to use HPC-wide working directory |
39 | | -#$ -wd ~/tmp |
| 92 | +echo "Job submission declarations:" |
| 93 | +<% |
| 94 | + writeLines(sprintf("echo '%s'", job_declarations)) |
| 95 | +%> |
40 | 96 |
|
41 | | -## SPECIAL: Since we change working directory, the 'startup.Rs' file used |
42 | | -## by R CMD check is no longer found |
43 | | -export R_TESTS= |
| 97 | +<% if (details) { %> |
| 98 | +if command -v qstat > /dev/null; then |
| 99 | + echo "Job information:" |
| 100 | + qstat -j "${JOB_ID}" |
| 101 | + echo |
| 102 | +fi |
| 103 | +<% } %> |
44 | 104 |
|
45 | | -# For troubleshooting if there are errors |
46 | | -date |
47 | | -hostname |
48 | | -which Rscript |
49 | | -Rscript --version |
50 | | -Rscript -e ".libPaths()" |
51 | 105 |
|
| 106 | +## Launch R and evaluate the batchtools R job |
52 | 107 | echo "Command: Rscript -e 'batchtools::doJobCollection("<%= uri %>")' ..." |
53 | 108 | Rscript -e 'batchtools::doJobCollection("<%= uri %>")' |
| 109 | +res=$? |
| 110 | +echo " - exit code: ${res}" |
54 | 111 | echo "Command: Rscript -e 'batchtools::doJobCollection("<%= uri %>")' ... done" |
55 | 112 |
|
56 | | -## For troubleshooting if there are errors |
57 | | -## https://github.com/UCSF-HPC/pilot-testing/issues/1 |
58 | | -qstat -j $JOB_ID |
| 113 | +<% if (details) { %> |
| 114 | +if command -v qstat > /dev/null; then |
| 115 | + echo "Job summary:" |
| 116 | + qstat -j "${JOB_ID}" |
| 117 | +fi |
| 118 | +<% } %> |
| 119 | + |
| 120 | +<% if (length(shutdown) > 0) { |
| 121 | + writeLines(shutdown) |
| 122 | +} %> |
| 123 | + |
| 124 | +## Relay the exit code from Rscript |
| 125 | +exit "${res}" |
0 commit comments