Skip to content
Snippets Groups Projects
Commit 4e655ae8 authored by Bob Mottram's avatar Bob Mottram
Browse files

Tidying cron utils

parent 34e59ae0
No related branches found
No related tags found
No related merge requests found
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
function cron_add_mins { function cron_add_mins {
if ! grep -q "${2}" /etc/crontab; then if ! grep -q "${2}" /etc/crontab; then
job_user='root' job_user='root'
if [ $3 ]; then if [ "$3" ]; then
job_user=$3 job_user=$3
fi fi
echo "*/${1} * * * * ${job_user} ${2}" >> /etc/crontab echo "*/${1} * * * * ${job_user} ${2}" >> /etc/crontab
...@@ -48,29 +48,29 @@ function randomize_cron { ...@@ -48,29 +48,29 @@ function randomize_cron {
fi fi
# randomize the day on which the weekly cron job runs # randomize the day on which the weekly cron job runs
randdow=$(($RANDOM%6+1)) randdow=$((RANDOM%6+1))
sed -i "s|\* \* 7|* * $randdow|g" /etc/crontab sed -i "s|\\* \\* 7|* * $randdow|g" /etc/crontab
# randomize the time when the weekly cron job runs # randomize the time when the weekly cron job runs
randmin=$(($RANDOM%60)) randmin=$((RANDOM%60))
randhr=$(($RANDOM%3+1)) randhr=$((RANDOM%3+1))
sed -i "s|47 6|$randmin $randhr|g" /etc/crontab sed -i "s|47 6|$randmin $randhr|g" /etc/crontab
# randomize the time when the daily cron job runs # randomize the time when the daily cron job runs
randmin=$(($RANDOM%60)) randmin=$((RANDOM%60))
randhr=$(($RANDOM%3+4)) randhr=$((RANDOM%3+4))
sed -i "s|25 6\t\* \* \*|$randmin $randhr\t* * *|g" /etc/crontab sed -i "s|25 6\\t\\* \\* \\*|$randmin $randhr\\t* * *|g" /etc/crontab
# randomize the time when the hourly cron job runs # randomize the time when the hourly cron job runs
randmin=$(($RANDOM%60)) randmin=$((RANDOM%60))
sed -i "s|17 \*\t|$randmin *\t|g" /etc/crontab sed -i "s|17 \\*\\t|$randmin *\\t|g" /etc/crontab
# randomize monthly cron job time and day # randomize monthly cron job time and day
randmin=$(($RANDOM%60)) randmin=$((RANDOM%60))
randhr=$(($RANDOM%22+1)) randhr=$((RANDOM%22+1))
randdom=$(($RANDOM%27+1)) randdom=$((RANDOM%27+1))
sed -i "s|52 6\t|$randmin $randhr\t|g" /etc/crontab sed -i "s|52 6\\t|$randmin $randhr\\t|g" /etc/crontab
sed -i "s|\t1 \* \*|\t$randdom * *|g" /etc/crontab sed -i "s|\\t1 \\* \\*|\\t$randdom * *|g" /etc/crontab
systemctl restart cron systemctl restart cron
...@@ -79,17 +79,17 @@ function randomize_cron { ...@@ -79,17 +79,17 @@ function randomize_cron {
function schedule_stig_tests { function schedule_stig_tests {
stig_tests_script=/tmp/stig_tests_script stig_tests_script=/tmp/stig_tests_script
echo '#!/bin/bash' > $stig_tests_script { echo '#!/bin/bash';
echo "ADMIN_EMAIL_ADDRESS=${MY_USERNAME}@\${HOSTNAME}" >> $stig_tests_script echo "ADMIN_EMAIL_ADDRESS=${MY_USERNAME}@\${HOSTNAME}";
echo "pkill ${PROJECT_NAME}-tests" >> $stig_tests_script echo "pkill ${PROJECT_NAME}-tests";
echo 'rm -rf /tmp/*' >> $stig_tests_script echo 'rm -rf /tmp/*';
echo "${PROJECT_NAME}-tests --stig yes > /tmp/daily-stig-tests" >> $stig_tests_script echo "${PROJECT_NAME}-tests --stig yes > /tmp/daily-stig-tests";
echo 'if [ ! "$?" = "0" ]; then' >> $stig_tests_script echo 'if [ ! "$?" = "0" ]; then';
echo " echo \"\$(cat /tmp/daily-stig-tests)\" | mail -s \"${PROJECT_NAME} STIG test failures\" \$ADMIN_EMAIL_ADDRESS" >> $stig_tests_script echo " echo \"\$(cat /tmp/daily-stig-tests)\" | mail -s \"${PROJECT_NAME} STIG test failures\" \$ADMIN_EMAIL_ADDRESS";
echo 'fi' >> $stig_tests_script echo 'fi';
echo 'if [ -f /tmp/daily-stig-tests ]; then' >> $stig_tests_script echo 'if [ -f /tmp/daily-stig-tests ]; then';
echo ' rm /tmp/daily-stig-tests' >> $stig_tests_script echo ' rm /tmp/daily-stig-tests';
echo 'fi' >> $stig_tests_script echo 'fi'; } > $stig_tests_script
chmod +x $stig_tests_script chmod +x $stig_tests_script
if [ ! -f /etc/cron.daily/stig_tests ]; then if [ ! -f /etc/cron.daily/stig_tests ]; then
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment