Last active
August 11, 2022 22:55
-
-
Save anselmobd/a8a3e2f87dd9b67121da78495093e751 to your computer and use it in GitHub Desktop.
Executes a command, stores its output and prints the differences compared to the last execution (for log)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
last_out="$1" | |
shift | |
exec_cmd="$@" | |
script_date=$(date) | |
date_to_file=$(date '+%Y-%m-%d_%H.%M.%S') | |
base_dir=$(dirname ${last_out}) | |
name_out=$(basename ${last_out}) | |
file_suffix="/tmp/cmd_out_log_${name_out}" | |
if [[ ! -d "${base_dir}" ]] ; then | |
mkdir -p "${base_dir}" | |
fi | |
if [ ! -e "${last_out}" ] ; then | |
touch "${last_out}" | |
fi | |
out_file="${file_suffix}.out" | |
dif_file="${file_suffix}.dif" | |
rm ${out_file}.* 2> /dev/null | |
rm ${dif_file}.* 2> /dev/null | |
out_tmp=$(mktemp "${out_file}_${date_to_file}.XXXXXXXXX") | |
dif_tmp=$(mktemp "${dif_file}_${date_to_file}.XXXXXXXXX") | |
${exec_cmd} > ${out_tmp} | |
diff ${out_tmp} ${last_out} &> ${dif_tmp} | |
if (( $? == 0 )) ; then | |
rm ${out_tmp} | |
else | |
mv ${out_tmp} ${last_out} | |
echo ${script_date} | |
cat ${dif_tmp} | |
fi | |
rm ${dif_tmp} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Examples of use in crontab