Skip to content

Commit 00fbf12

Browse files
committed
Improve cron support
Add functions - cron_add() - cron_remove() which will be available in custom user/project startup scripts
1 parent 0420235 commit 00fbf12

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

Dockerfiles/base/data/docker-entrypoint.d/100-base-libs.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ set -o pipefail
1313
### Log to stdout/stderr
1414
###
1515
log() {
16+
# prevent message from being expanded
17+
set -o noglob
18+
1619
local type="${1}" # ok, warn or err
1720
local message="${2}" # msg to print
1821
local debug="${3}" # 0: only warn and error, >0: ok and info
@@ -38,7 +41,10 @@ log() {
3841
else
3942
printf "${clr_err}[???] %s${clr_rst}\n" "${message}" 1>&2 # stdout -> stderr
4043
fi
44+
45+
set +o noglob
4146
}
47+
export -f log
4248

4349

4450
###
@@ -57,7 +63,7 @@ run() {
5763
fi
5864
/bin/sh -c "LANG=C LC_ALL=C ${cmd}"
5965
}
60-
66+
export -f run
6167

6268
###
6369
### Is argument a positive integer?
@@ -73,7 +79,7 @@ isint() {
7379
env_set() {
7480
printenv "${1}" >/dev/null 2>&1
7581
}
76-
82+
export -f env_set
7783

7884
###
7985
### Get env variable by name
@@ -91,6 +97,7 @@ env_get() {
9197
# Just output the env value
9298
printenv "${1}"
9399
}
100+
export -f env_get
94101

95102

96103
############################################################
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
set -u
5+
set -o pipefail
6+
7+
8+
###########################################################
9+
# Functions
10+
############################################################
11+
12+
# add line to devilbox's crontab, if not already there
13+
# and start cron service
14+
cron_add() {
15+
# save the entire line in one variable
16+
line="$*"
17+
18+
DEBUG_LEVEL="$( env_get "DEBUG_ENTRYPOINT" "0" )"
19+
20+
# check if line already exists in crontab
21+
crontab -l -u devilbox | grep "$line" > /dev/null
22+
status=$?
23+
24+
if [ $status -ne 0 ]
25+
then
26+
log "info" "cron: adding line '${line}' ..." "$DEBUG_LEVEL"
27+
(crontab -l -u devilbox; echo "$line";) | crontab -u devilbox -
28+
fi
29+
30+
# make sure the cron service is running
31+
if ! service cron status >/dev/null
32+
then
33+
service cron start
34+
fi
35+
}
36+
export -f cron_add
37+
38+
cron_remove() {
39+
# save the entire line in one variable
40+
line=$*
41+
42+
DEBUG_LEVEL="$( env_get "DEBUG_ENTRYPOINT" "0" )"
43+
44+
# check if line already exists in crontab
45+
crontab -l -u devilbox | grep "$line" > /dev/null
46+
status=$?
47+
48+
if [ $status -eq 0 ]; then
49+
log "info" "cron: removing line '${line}' ..." "$DEBUG_LEVEL"
50+
(crontab -l -u devilbox | grep -v "$line";) | crontab -u devilbox -
51+
fi
52+
}
53+
export -f cron_remove

Dockerfiles/prod/data/docker-entrypoint.d/310-custom-startup-scripts.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ set -o pipefail
1010
############################################################
1111

1212
###
13-
### Execute custom uesr-supplied scripts
13+
### Execute custom user-supplied scripts
1414
###
1515
execute_custom_scripts() {
1616
local script_dir="${1}"
@@ -27,7 +27,7 @@ execute_custom_scripts() {
2727
for script_f in ${script_files}; do
2828
script_name="$( basename "${script_f}" )"
2929
log "info" "Executing custom startup script: ${script_name}" "${debug}"
30-
if ! bash "${script_f}"; then
30+
if ! bash "${script_f}" "${debug}"; then
3131
log "err" "Failed to execute script" "${debug}"
3232
exit 1
3333
fi
@@ -43,6 +43,10 @@ if ! command -v find >/dev/null 2>&1; then
4343
echo "find not found, but required."
4444
exit 1
4545
fi
46+
if ! command -v sort >/dev/null 2>&1; then
47+
echo "sort not found, but required."
48+
exit 1
49+
fi
4650
if ! command -v basename >/dev/null 2>&1; then
4751
echo "basename not found, but required."
4852
exit 1

0 commit comments

Comments
 (0)