I need to check for the current time and abort the script if it is not proper time of day I am supposed to run it. Also, if anyone else runs it, it should abort.
For example: I need my script to only run if it is started between 10 PM and and 2 AM (a 4 hour window).
Currently I am doing the following. Taking the time with date +%k%M
and comparing with hard coded numbers
#!/bin/sh
currTime=`date +%k%M`
check_time_to_run()
{
tempTime=$1
if [ $tempTime -gt 200 -a $tempTime -lt 2200 ]; then
echo "Time is between 2 AM and 10 PM. Aborting."
exit 1
else
echo "Time is after 10 PM and before 2 AM. Running normally."
fi
}
check_time_to_run $currentTime
I would like to know if there is any other efficient method with respect to time comparision in sh
or bash
?
date +%s
.currTime=$(date +%s); lower=$(date +%s --date="2:00"); upper=$(date +%s --date="22:00"); if [$currTime -gt $lower -a $currTime -lt $upper]; then
…faketime
, your script is not very safe. Using lockfiles is probably a better option.