Tidying cron utils

This commit is contained in:
Bob Mottram 2018-02-25 15:16:23 +00:00
parent 34e59ae030
commit 4e655ae8da
1 changed files with 26 additions and 26 deletions

View File

@ -31,7 +31,7 @@
function cron_add_mins {
if ! grep -q "${2}" /etc/crontab; then
job_user='root'
if [ $3 ]; then
if [ "$3" ]; then
job_user=$3
fi
echo "*/${1} * * * * ${job_user} ${2}" >> /etc/crontab
@ -48,29 +48,29 @@ function randomize_cron {
fi
# randomize the day on which the weekly cron job runs
randdow=$(($RANDOM%6+1))
sed -i "s|\* \* 7|* * $randdow|g" /etc/crontab
randdow=$((RANDOM%6+1))
sed -i "s|\\* \\* 7|* * $randdow|g" /etc/crontab
# randomize the time when the weekly cron job runs
randmin=$(($RANDOM%60))
randhr=$(($RANDOM%3+1))
randmin=$((RANDOM%60))
randhr=$((RANDOM%3+1))
sed -i "s|47 6|$randmin $randhr|g" /etc/crontab
# randomize the time when the daily cron job runs
randmin=$(($RANDOM%60))
randhr=$(($RANDOM%3+4))
sed -i "s|25 6\t\* \* \*|$randmin $randhr\t* * *|g" /etc/crontab
randmin=$((RANDOM%60))
randhr=$((RANDOM%3+4))
sed -i "s|25 6\\t\\* \\* \\*|$randmin $randhr\\t* * *|g" /etc/crontab
# randomize the time when the hourly cron job runs
randmin=$(($RANDOM%60))
sed -i "s|17 \*\t|$randmin *\t|g" /etc/crontab
randmin=$((RANDOM%60))
sed -i "s|17 \\*\\t|$randmin *\\t|g" /etc/crontab
# randomize monthly cron job time and day
randmin=$(($RANDOM%60))
randhr=$(($RANDOM%22+1))
randdom=$(($RANDOM%27+1))
sed -i "s|52 6\t|$randmin $randhr\t|g" /etc/crontab
sed -i "s|\t1 \* \*|\t$randdom * *|g" /etc/crontab
randmin=$((RANDOM%60))
randhr=$((RANDOM%22+1))
randdom=$((RANDOM%27+1))
sed -i "s|52 6\\t|$randmin $randhr\\t|g" /etc/crontab
sed -i "s|\\t1 \\* \\*|\\t$randdom * *|g" /etc/crontab
systemctl restart cron
@ -79,17 +79,17 @@ function randomize_cron {
function schedule_stig_tests {
stig_tests_script=/tmp/stig_tests_script
echo '#!/bin/bash' > $stig_tests_script
echo "ADMIN_EMAIL_ADDRESS=${MY_USERNAME}@\${HOSTNAME}" >> $stig_tests_script
echo "pkill ${PROJECT_NAME}-tests" >> $stig_tests_script
echo 'rm -rf /tmp/*' >> $stig_tests_script
echo "${PROJECT_NAME}-tests --stig yes > /tmp/daily-stig-tests" >> $stig_tests_script
echo 'if [ ! "$?" = "0" ]; then' >> $stig_tests_script
echo " echo \"\$(cat /tmp/daily-stig-tests)\" | mail -s \"${PROJECT_NAME} STIG test failures\" \$ADMIN_EMAIL_ADDRESS" >> $stig_tests_script
echo 'fi' >> $stig_tests_script
echo 'if [ -f /tmp/daily-stig-tests ]; then' >> $stig_tests_script
echo ' rm /tmp/daily-stig-tests' >> $stig_tests_script
echo 'fi' >> $stig_tests_script
{ echo '#!/bin/bash';
echo "ADMIN_EMAIL_ADDRESS=${MY_USERNAME}@\${HOSTNAME}";
echo "pkill ${PROJECT_NAME}-tests";
echo 'rm -rf /tmp/*';
echo "${PROJECT_NAME}-tests --stig yes > /tmp/daily-stig-tests";
echo 'if [ ! "$?" = "0" ]; then';
echo " echo \"\$(cat /tmp/daily-stig-tests)\" | mail -s \"${PROJECT_NAME} STIG test failures\" \$ADMIN_EMAIL_ADDRESS";
echo 'fi';
echo 'if [ -f /tmp/daily-stig-tests ]; then';
echo ' rm /tmp/daily-stig-tests';
echo 'fi'; } > $stig_tests_script
chmod +x $stig_tests_script
if [ ! -f /etc/cron.daily/stig_tests ]; then