From 9e43d6404f65b7909d9171c4324413f636ddc579 Mon Sep 17 00:00:00 2001
From: Marco Malavolti <marco.malavolti@gmail.com>
Date: Tue, 28 Jul 2020 14:41:31 +0200
Subject: [PATCH] Fixed CRON job & log

---
 README.md           |  2 ++
 cleanAndRunEccs2.sh | 66 ++++++++++++++++++++++++++-------------------
 2 files changed, 41 insertions(+), 27 deletions(-)

diff --git a/README.md b/README.md
index 29c4b63..1a30f72 100644
--- a/README.md
+++ b/README.md
@@ -191,6 +191,8 @@ Note: Pay attetion on the chromedriver version:
    * `crontab -e`
 
      ```bash
+     SHELL=/bin/bash
+
      0 4 * * * /bin/bash $HOME/eccs2/cleanAndRunEccs2.sh > $HOME/eccs2/logs/eccs2cron.log 2>&1  
      ```
 
diff --git a/cleanAndRunEccs2.sh b/cleanAndRunEccs2.sh
index 716d3b0..9026204 100755
--- a/cleanAndRunEccs2.sh
+++ b/cleanAndRunEccs2.sh
@@ -1,14 +1,16 @@
 #!/bin/bash
 
+source $HOME/.bash_profile
+
 # logs/stderr_$date.log is kept to see which IdP had been errors
 
 BASEDIR=$HOME
 
 # Remove old IdP and Fed List
-rm -f $BASEDIR/eccs2/input/*.json
+#rm -f $BASEDIR/eccs2/input/*.json
 
 # Run ECCS2
-$BASEDIR/eccs2/runEccs2.py
+#$BASEDIR/eccs2/runEccs2.py
 
 # Run Failed Command again
 bash $BASEDIR/eccs2/logs/failed-cmd.sh
@@ -21,34 +23,44 @@ suffix="'"
 eccs2output="$BASEDIR/eccs2/output/eccs2_$date.log"
 declare -a eccs2cmdToRemoveArray
 
-while IFS= read -r line
-do
-	string=$line
+if [ -s $file ]; then
+
+   while IFS= read -r line
+   do
+      string=$line
 
-   #remove "prefix" from the command string at the beginning.
-   prefix_removed_string=${string/#$prefix}
+      #remove "prefix" from the command string at the beginning.
+      prefix_removed_string=${string/#$prefix}
 
-   #remove "suffix" from the command string at the end.
-   suffix_removed_string=${prefix_removed_string/%$suffix}
+      #remove "suffix" from the command string at the end.
+      suffix_removed_string=${prefix_removed_string/%$suffix}
 
-   entityIDidp=$(echo "$suffix_removed_string" | jq '.entityID')
+      entityIDidp=$(echo "$suffix_removed_string" | jq '.entityID')
 
-   #remove start and end quotes from the entityIDidp to be able to use "grep"
-   entityIDidp="${entityIDidp:1}"
-   entityIDidp="${entityIDidp%?}"
+      #remove start and end quotes from the entityIDidp to be able to use "grep"
+      entityIDidp="${entityIDidp:1}"
+      entityIDidp="${entityIDidp%?}"
 
-   result=$(grep $entityIDidp $eccs2output | wc -l)
+      result=$(grep $entityIDidp $eccs2output | wc -l)
    
-   if [[ "$result" = 1 ]]; then
-      eccs2cmdToRemoveArray+=("$entityIDidp")
-   else
-      echo "The result for the IdP '$entityIDidp' has been found multiple times on $eccs2output. It is wrong."
-   fi
-
-done <"$file"
-
-# Remove IdP command that had success from "failed-cmd.sh"
-for idpToRemove in ${eccs2cmdToRemoveArray[@]}
-do
-    $(grep -v $idpToRemove $file > temp ; mv -f temp $file)
-done
+      if [[ "$result" = 1 ]]; then
+         eccs2cmdToRemoveArray+=("$entityIDidp")
+      else
+         echo "The result for the IdP '$entityIDidp' has been found multiple times on $eccs2output. It is wrong."
+      fi
+
+   done <"$file"
+
+
+   # Remove IdP command that had success from "failed-cmd.sh"
+   for idpToRemove in ${eccs2cmdToRemoveArray[@]}
+   do
+      $(grep -v $idpToRemove $file > temp ; mv -f temp $file)
+   done
+fi
+
+if [ -s $file ]; then
+   echo "$date - ECCS2 NOT OK: Some eduGAIN IdPs have remained unchecked. See the 'logs/failed-cmd.sh' and logs/stderr_$date.log files"
+else
+   echo "$date - ECCS2 OK: All eduGAIN IdPs have been checked successfully"
+fi
-- 
GitLab