Guest User

Untitled

a guest
Jun 26th, 2018
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #!/bin/bash
  2. # Bash Script to mae sur script runs at specific time, and should not run again for the same date
  3. # record date in local file, to avoic repeating running the code in todays date, once the date is changed,
  4. # then re-run the code and match time again and so on
  5. # Syed Jahanzaib / 26-Jun-2018
  6. # set -x
  7.  
  8. # Setting variables
  9. DATE=$(date +%d-%m-%Y)
  10. FULL_DATE=`date`
  11. FILE=/temp/1.txt
  12. touch $FILE
  13. CURR_HOUR=$(date +%H)
  14.  
  15. # Set time for script execution
  16. SCR_SCHEDULED_TIME="11"
  17. H=$(date +'%-H')
  18.  
  19. CHK_GREP=`grep -c $DATE $FILE`
  20. echo "Current Date time is $FULL_DATE"
  21.  
  22. # If script is executed successfully then dont re-run and exit now
  23. if grep -q $DATE $FILE >/dev/null 2>&1
  24. then
  25. echo "It seems the script was executed successfully today $DATE, It will run on next date change.... Exiting now."
  26. exit 1
  27. fi
  28.  
  29. # Check if time is matched that is greater or equals to $SCR_SCHEDULED_TIME and also check if script hae ran successfully or not previously
  30. echo "
  31. Stage-1: Checking if current time is equals or greater then '$SCR_SCHEDULED_TIME hours' ..."
  32. if [ "$CURR_HOUR" -ge "$SCR_SCHEDULED_TIME" ] && [ "$CHK_GREP" == "0" ]; then
  33. echo "
  34. Stage-2: Time matched that is equals or greater then $SCR_SCHEDULED_TIME"
  35. # If all matches, then run the code ! and add time stamp in file to avoid repeatingo/re-running the following script code on next RUN
  36. echo "Finally: All conditions time + this day first execution matched, Now running the script code ...."
  37. date +%d-%m-%Y >> $FILE
  38. exit 1
  39. fi
  40.  
  41. # If time have not come like its before the $SCR_SCHEDULED_TIME, then give error and exit now
  42. echo "
  43. Warning !
  44. Scheudled time is equals or greater then $SCR_SCHEDULED_TIME hours : Current hour is $CURR_HOUR
  45. Time have not came yet ! wait for your turn ..."
Add Comment
Please, Sign In to add comment