Skip to content

Commit 1abdd06

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 1abdd06

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ set -o pipefail
1313
### Log to stdout/stderr
1414
###
1515
log() {
16+
set -o noglob # prevent message from being expanded
17+
1618
local type="${1}" # ok, warn or err
1719
local message="${2}" # msg to print
1820
local debug="${3}" # 0: only warn and error, >0: ok and info
@@ -38,7 +40,10 @@ log() {
3840
else
3941
printf "${clr_err}[???] %s${clr_rst}\n" "${message}" 1>&2 # stdout -> stderr
4042
fi
43+
44+
set +o noglob
4145
}
46+
export -f log
4247

4348

4449
###
@@ -57,7 +62,7 @@ run() {
5762
fi
5863
/bin/sh -c "LANG=C LC_ALL=C ${cmd}"
5964
}
60-
65+
export -f run
6166

6267
###
6368
### Is argument a positive integer?
@@ -73,7 +78,7 @@ isint() {
7378
env_set() {
7479
printenv "${1}" >/dev/null 2>&1
7580
}
76-
81+
export -f env_set
7782

7883
###
7984
### Get env variable by name
@@ -91,6 +96,7 @@ env_get() {
9196
# Just output the env value
9297
printenv "${1}"
9398
}
99+
export -f env_get
94100

95101

96102
############################################################
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

+6-2
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)