Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Bash Script to mae sur script runs at specific time, and should not run again for the same date
- # record date in local file, to avoic repeating running the code in todays date, once the date is changed,
- # then re-run the code and match time again and so on
- # Syed Jahanzaib / 26-Jun-2018
- # set -x
- # Setting variables
- DATE=$(date +%d-%m-%Y)
- FULL_DATE=`date`
- FILE=/temp/1.txt
- touch $FILE
- CURR_HOUR=$(date +%H)
- # Set time for script execution
- SCR_SCHEDULED_TIME="11"
- H=$(date +'%-H')
- CHK_GREP=`grep -c $DATE $FILE`
- echo "Current Date time is $FULL_DATE"
- # If script is executed successfully then dont re-run and exit now
- if grep -q $DATE $FILE >/dev/null 2>&1
- then
- echo "It seems the script was executed successfully today $DATE, It will run on next date change.... Exiting now."
- exit 1
- fi
- # 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
- echo "
- Stage-1: Checking if current time is equals or greater then '$SCR_SCHEDULED_TIME hours' ..."
- if [ "$CURR_HOUR" -ge "$SCR_SCHEDULED_TIME" ] && [ "$CHK_GREP" == "0" ]; then
- echo "
- Stage-2: Time matched that is equals or greater then $SCR_SCHEDULED_TIME"
- # 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
- echo "Finally: All conditions time + this day first execution matched, Now running the script code ...."
- date +%d-%m-%Y >> $FILE
- exit 1
- fi
- # If time have not come like its before the $SCR_SCHEDULED_TIME, then give error and exit now
- echo "
- Warning !
- Scheudled time is equals or greater then $SCR_SCHEDULED_TIME hours : Current hour is $CURR_HOUR
- Time have not came yet ! wait for your turn ..."
Add Comment
Please, Sign In to add comment