Skip to content

Commit 71f3d31

Browse files
committed
Add new parameter to optimise for running under Cron
1 parent 570204f commit 71f3d31

File tree

3 files changed

+110
-12
lines changed

3 files changed

+110
-12
lines changed

script.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Usage:
2222
-h|--help Displays this help
2323
-v|--verbose Displays verbose output
2424
-nc|--no-colour Disables colour output
25+
-cr|--cron Run silently unless we encounter an error
2526
EOF
2627
}
2728

@@ -44,6 +45,9 @@ function parse_params() {
4445
-nc|--no-colour)
4546
no_colour="true"
4647
;;
48+
-cr|--cron)
49+
cron="true"
50+
;;
4751
*)
4852
script_exit "Invalid parameter was provided: $param" 2
4953
;;
@@ -61,8 +65,9 @@ function main() {
6165
trap "script_trap_err" ERR
6266
trap "script_trap_exit" EXIT
6367

64-
script_init
68+
script_init "$@"
6569
parse_params "$@"
70+
cron_init
6671
colour_init
6772
}
6873

source.sh

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,46 @@
1010
# DESC: Handler for unexpected errors
1111
# ARGS: $1 (optional): Exit code (defaults to 1)
1212
function script_trap_err() {
13+
local exit_code=1
14+
1315
# Disable the error trap handler to prevent potential recursion
1416
trap - ERR
1517

1618
# Consider any further errors non-fatal to ensure we run to completion
1719
set +o errexit
1820
set +o pipefail
1921

20-
# Exit with failure status
22+
# Validate any provided exit code
2123
if [[ $# -eq 1 && $1 =~ ^[0-9]+$ ]]; then
22-
exit "$1"
23-
else
24-
exit 1
24+
exit_code="$1"
25+
fi
26+
27+
# Output debug data if in Cron mode
28+
if [[ -n ${cron-} ]]; then
29+
# Restore original file output descriptors
30+
if [[ -n ${script_output-} ]]; then
31+
exec 1>&3 2>&4
32+
fi
33+
34+
# Print basic debugging information
35+
printf '%b\n' "$ta_none"
36+
printf '***** Abnormal termination of script *****\n'
37+
printf 'Script Path: %s\n' "$script_path"
38+
printf 'Script Parameters: %s\n' "$script_params"
39+
printf 'Script Exit Code: %s\n' "$exit_code"
40+
41+
# Print the script log if we have it. It's possible we may not if we
42+
# failed before we even called cron_init(). This can happen if bad
43+
# parameters were passed to the script so we bailed out very early.
44+
if [[ -n ${script_output-} ]]; then
45+
printf 'Script Output:\n\n%s' "$(cat "$script_output")"
46+
else
47+
printf 'Script Output: None (failed before log init)\n'
48+
fi
2549
fi
50+
51+
# Exit with failure status
52+
exit "$exit_code"
2653
}
2754

2855

@@ -31,6 +58,11 @@ function script_trap_err() {
3158
function script_trap_exit() {
3259
cd "$orig_cwd"
3360

61+
# Remove Cron mode script log
62+
if [[ -n ${cron-} && -f ${script_output-} ]]; then
63+
rm "$script_output"
64+
fi
65+
3466
# Restore terminal colours
3567
printf '%b' "$ta_none"
3668
}
@@ -60,13 +92,14 @@ function script_exit() {
6092

6193

6294
# DESC: Generic script initialisation
63-
# ARGS: None
95+
# ARGS: $@ (optional): Arguments provided to the script
6496
function script_init() {
6597
# Useful paths
6698
readonly orig_cwd="$PWD"
6799
readonly script_path="${BASH_SOURCE[0]}"
68100
readonly script_dir="$(dirname "$script_path")"
69101
readonly script_name="$(basename "$script_path")"
102+
readonly script_params="$*"
70103

71104
# Important to always set as we use it in the exit handler
72105
readonly ta_none="$(tput sgr0 || true)"
@@ -155,6 +188,17 @@ function colour_init() {
155188
}
156189

157190

191+
# DESC: Initialise Cron mode
192+
# ARGS: None
193+
function cron_init() {
194+
if [[ -n ${cron-} ]]; then
195+
# Redirect all output to a temporary file
196+
readonly script_output="$(mktemp --tmpdir "$script_name".XXXXX)"
197+
exec 3>&1 4>&2 1>"$script_output" 2>&1
198+
fi
199+
}
200+
201+
158202
# DESC: Pretty print the provided string
159203
# ARGS: $1 (required): Message to print (defaults to a green foreground)
160204
# $2 (optional): Colour to print the message with. This can be an ANSI

template.sh

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,46 @@ set -o pipefail # Use last non-zero exit code in a pipeline
1414
# DESC: Handler for unexpected errors
1515
# ARGS: $1 (optional): Exit code (defaults to 1)
1616
function script_trap_err() {
17+
local exit_code=1
18+
1719
# Disable the error trap handler to prevent potential recursion
1820
trap - ERR
1921

2022
# Consider any further errors non-fatal to ensure we run to completion
2123
set +o errexit
2224
set +o pipefail
2325

24-
# Exit with failure status
26+
# Validate any provided exit code
2527
if [[ $# -eq 1 && $1 =~ ^[0-9]+$ ]]; then
26-
exit "$1"
27-
else
28-
exit 1
28+
exit_code="$1"
2929
fi
30+
31+
# Output debug data if in Cron mode
32+
if [[ -n ${cron-} ]]; then
33+
# Restore original file output descriptors
34+
if [[ -n ${script_output-} ]]; then
35+
exec 1>&3 2>&4
36+
fi
37+
38+
# Print basic debugging information
39+
printf '%b\n' "$ta_none"
40+
printf '***** Abnormal termination of script *****\n'
41+
printf 'Script Path: %s\n' "$script_path"
42+
printf 'Script Parameters: %s\n' "$script_params"
43+
printf 'Script Exit Code: %s\n' "$exit_code"
44+
45+
# Print the script log if we have it. It's possible we may not if we
46+
# failed before we even called cron_init(). This can happen if bad
47+
# parameters were passed to the script so we bailed out very early.
48+
if [[ -n ${script_output-} ]]; then
49+
printf 'Script Output:\n\n%s' "$(cat "$script_output")"
50+
else
51+
printf 'Script Output: None (failed before log init)\n'
52+
fi
53+
fi
54+
55+
# Exit with failure status
56+
exit "$exit_code"
3057
}
3158

3259

@@ -35,6 +62,11 @@ function script_trap_err() {
3562
function script_trap_exit() {
3663
cd "$orig_cwd"
3764

65+
# Remove Cron mode script log
66+
if [[ -n ${cron-} && -f ${script_output-} ]]; then
67+
rm "$script_output"
68+
fi
69+
3870
# Restore terminal colours
3971
printf '%b' "$ta_none"
4072
}
@@ -64,13 +96,14 @@ function script_exit() {
6496

6597

6698
# DESC: Generic script initialisation
67-
# ARGS: None
99+
# ARGS: $@ (optional): Arguments provided to the script
68100
function script_init() {
69101
# Useful paths
70102
readonly orig_cwd="$PWD"
71103
readonly script_path="${BASH_SOURCE[0]}"
72104
readonly script_dir="$(dirname "$script_path")"
73105
readonly script_name="$(basename "$script_path")"
106+
readonly script_params="$*"
74107

75108
# Important to always set as we use it in the exit handler
76109
readonly ta_none="$(tput sgr0 || true)"
@@ -159,6 +192,17 @@ function colour_init() {
159192
}
160193

161194

195+
# DESC: Initialise Cron mode
196+
# ARGS: None
197+
function cron_init() {
198+
if [[ -n ${cron-} ]]; then
199+
# Redirect all output to a temporary file
200+
readonly script_output="$(mktemp --tmpdir "$script_name".XXXXX)"
201+
exec 3>&1 4>&2 1>"$script_output" 2>&1
202+
fi
203+
}
204+
205+
162206
# DESC: Pretty print the provided string
163207
# ARGS: $1 (required): Message to print (defaults to a green foreground)
164208
# $2 (optional): Colour to print the message with. This can be an ANSI
@@ -317,6 +361,7 @@ Usage:
317361
-h|--help Displays this help
318362
-v|--verbose Displays verbose output
319363
-nc|--no-colour Disables colour output
364+
-cr|--cron Run silently unless we encounter an error
320365
EOF
321366
}
322367

@@ -339,6 +384,9 @@ function parse_params() {
339384
-nc|--no-colour)
340385
no_colour="true"
341386
;;
387+
-cr|--cron)
388+
cron="true"
389+
;;
342390
*)
343391
script_exit "Invalid parameter was provided: $param" 2
344392
;;
@@ -353,8 +401,9 @@ function main() {
353401
trap "script_trap_err" ERR
354402
trap "script_trap_exit" EXIT
355403

356-
script_init
404+
script_init "$@"
357405
parse_params "$@"
406+
cron_init
358407
colour_init
359408
}
360409

0 commit comments

Comments
 (0)