Script to install monitoring task.

This commit is contained in:
Stephane Gourichon
2024-03-24 17:13:58 +01:00
parent 55cdc5ac3e
commit 99e4b6e767
2 changed files with 74 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
#!/bin/bash
if [[ "$#" != 1 ]]
then
echo >&2 "usage:"
echo >&2 "$0 install"
echo >&2 "$0 uninstall"
exit 1
fi
if [[ "$1" == "install" ]]
then
INSTALL=true
elif [[ "$1" == "uninstall" ]]
then
INSTALL=false
else
echo >&2 "unknown argument : $1"
exit 1
fi
cd -P "$(dirname "$(readlink -f "$0")" )"
if [[ "$PWD" != "$(printf %q "$PWD" )" ]]
then
echo >&2 "WARNING: path seems unsafe: $PWD"
fi
SCRIPT_ROOT="$PWD"
cd ..
INFRA_ROOT="$PWD"
CRON_LINES=()
if [[ "$INSTALL" == true ]]
then
while IFS="" read -u 3 -r MONITOR_SCRIPT_PATH
do
if [[ "$MONITOR_SCRIPT_PATH" != "$(printf %q "$MONITOR_SCRIPT_PATH" )" ]]
then
echo >&2 "WARNING: path seems unsafe: $MONITOR_SCRIPT_PATH"
fi
CRON_LINES+=( "0 0 * * * $SCRIPT_ROOT/run_and_output_only_on_error.sh $INFRA_ROOT/$MONITOR_SCRIPT_PATH # monitoring_crontask" )
done 3< <( find */ -iname "monitor_check_once.sh" )
fi
{
crontab -l | fgrep -v "# monitoring_crontask"
printf '%s\n' "${CRON_LINES[@]}"
} | crontab -

View File

@@ -0,0 +1,22 @@
#!/bin/bash
set -eu
LOGFILENAME=$(mktemp /tmp/run_and_output_only_on_error_XXXXXX)
# set -xv
trap "rm -f ${LOGFILENAME}" EXIT
if
(
$@
) > "${LOGFILENAME}" 2>&1
then
true
else
echo
echo "Error on running: $@"
echo
echo "Here's the program's output (out and err combined)"
cat "${LOGFILENAME}"
fi