blob: 73fe94cc949bc5c0dd2525133be8c63cd55c4915 [file] [log] [blame]
#!/bin/sh
# Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Run tasks periodically.
# Usage: $0 <delay_seconds> <task_name> <task_binary>
#
# Executes task <task_name> by running <task_binary> every <delay_seconds>.
set -e -u
SCRIPT_NAME="$(basename "$0")"
CHECK_DELAY=300 # Check every 5 minutes.
KILL_DELAY=10 # How long to let the job clean up after a timeout.
SPOOL_DIR=/var/spool/cron-lite
log() {
logger -p local3.info -t "${SCRIPT_NAME}" "$@"
}
trap "log 'exiting'" EXIT
check_and_fix_spool_paths() {
# Avoid weird spool paths if possible.
test -L /var/spool && unlink /var/spool
test -L "${SPOOL_DIR}" && unlink "${SPOOL_DIR}"
mkdir -p "${SPOOL_DIR}"
if [ ! -O "${SPOOL_DIR}" -o ! -d "${SPOOL_DIR}" ]; then
log "Spool directory is damaged. Aborting!"
exit 1
fi
}
main() {
local delay="$1"
local name="$2"
local spool_file="${SPOOL_DIR}/${name}"
shift 2
[ -z "${delay}" ] && exit 1
[ -z "${name}" ] && exit 1
[ $# -eq 0 ] && exit 1
check_and_fix_spool_paths
while true; do
# Allow the sleep to be killed manually without terminating the handler.
sleep $(( CHECK_DELAY + KILL_DELAY )) || true
[ ! -e "${spool_file}" ] && touch "${spool_file}"
local last_rotation="$(stat -c "%Y" "${spool_file}" 2>/dev/null || echo 0)"
local now="$(date +%s)"
local time_diff=$((now - last_rotation))
if [ ${time_diff} -gt ${delay} ]; then
rm "${spool_file}" || true
touch "${spool_file}"
# Make sure to reclaim status info about background processes.
# At this point, all background processes should be dead.
# We don't actually care about the result though.
wait || true
log "${name}: running $*"
(
timeout -k ${KILL_DELAY} ${CHECK_DELAY} "$@" || true
log "${name}: job completed"
) &
fi
done
}
main "$@"