Skip to content

Commit

Permalink
Improve cron support
Browse files Browse the repository at this point in the history
Add functions
- cron_add()
- cron_remove()
which will be available in custom user/project startup scripts
  • Loading branch information
martin-rueegg committed Feb 23, 2023
1 parent 0420235 commit 00fbf12
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
11 changes: 9 additions & 2 deletions Dockerfiles/base/data/docker-entrypoint.d/100-base-libs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ set -o pipefail
### Log to stdout/stderr
###
log() {
# prevent message from being expanded
set -o noglob

local type="${1}" # ok, warn or err
local message="${2}" # msg to print
local debug="${3}" # 0: only warn and error, >0: ok and info
Expand All @@ -38,7 +41,10 @@ log() {
else
printf "${clr_err}[???] %s${clr_rst}\n" "${message}" 1>&2 # stdout -> stderr
fi

set +o noglob
}
export -f log


###
Expand All @@ -57,7 +63,7 @@ run() {
fi
/bin/sh -c "LANG=C LC_ALL=C ${cmd}"
}

export -f run

###
### Is argument a positive integer?
Expand All @@ -73,7 +79,7 @@ isint() {
env_set() {
printenv "${1}" >/dev/null 2>&1
}

export -f env_set

###
### Get env variable by name
Expand All @@ -91,6 +97,7 @@ env_get() {
# Just output the env value
printenv "${1}"
}
export -f env_get


############################################################
Expand Down
53 changes: 53 additions & 0 deletions Dockerfiles/base/data/docker-entrypoint.d/102-cron.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

set -e
set -u
set -o pipefail


###########################################################
# Functions
############################################################

# add line to devilbox's crontab, if not already there
# and start cron service
cron_add() {
# save the entire line in one variable
line="$*"

DEBUG_LEVEL="$( env_get "DEBUG_ENTRYPOINT" "0" )"

# check if line already exists in crontab
crontab -l -u devilbox | grep "$line" > /dev/null
status=$?

if [ $status -ne 0 ]
then
log "info" "cron: adding line '${line}' ..." "$DEBUG_LEVEL"
(crontab -l -u devilbox; echo "$line";) | crontab -u devilbox -
fi

# make sure the cron service is running
if ! service cron status >/dev/null
then
service cron start
fi
}
export -f cron_add

cron_remove() {
# save the entire line in one variable
line=$*

DEBUG_LEVEL="$( env_get "DEBUG_ENTRYPOINT" "0" )"

# check if line already exists in crontab
crontab -l -u devilbox | grep "$line" > /dev/null
status=$?

if [ $status -eq 0 ]; then
log "info" "cron: removing line '${line}' ..." "$DEBUG_LEVEL"
(crontab -l -u devilbox | grep -v "$line";) | crontab -u devilbox -
fi
}
export -f cron_remove
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set -o pipefail
############################################################

###
### Execute custom uesr-supplied scripts
### Execute custom user-supplied scripts
###
execute_custom_scripts() {
local script_dir="${1}"
Expand All @@ -27,7 +27,7 @@ execute_custom_scripts() {
for script_f in ${script_files}; do
script_name="$( basename "${script_f}" )"
log "info" "Executing custom startup script: ${script_name}" "${debug}"
if ! bash "${script_f}"; then
if ! bash "${script_f}" "${debug}"; then
log "err" "Failed to execute script" "${debug}"
exit 1
fi
Expand All @@ -43,6 +43,10 @@ if ! command -v find >/dev/null 2>&1; then
echo "find not found, but required."
exit 1
fi
if ! command -v sort >/dev/null 2>&1; then
echo "sort not found, but required."
exit 1
fi
if ! command -v basename >/dev/null 2>&1; then
echo "basename not found, but required."
exit 1
Expand Down

0 comments on commit 00fbf12

Please sign in to comment.