Run a job between times else run later

Hi guys,

I have written a script that waits for a trigger file.
Then checks the time of the trigger.
if the trigger finished between 8pm and midnight then runs a job.
else it waits till 1am then runs a different job.
I am still very new to scripting so any suggestions to improve my approach would be greatly appreciated. I tend to over complicate my approach and want to learn more.

while : ; do
    [[ -f "/tmp/TRIGGER" ]] && break
    sleep 1
done
checktime=`ls -lart /tmp | grep TRIGGER | awk '{ print $8 }' | sed 's/://'`
echo "$checktime"
mid='2359'
start='2000'

if [ "$checktime" -lt  "$mid ] && [ "$checktime" -gt "$start" ]
then
Astats1.sh
else
while [ $(date +%H:%M) != "01:01" ]; do sleep 1; done
Bstats1.sh 1
fi

What system do you use? Does it have the stat command (or similar) providing file statistical info?
Why do you break the while loop instead of using the -f test's result?
Do you REALLY need to test every single second? Wouldn't 10, 30 or even 300 suffice?
When does this script run? Anytime during the day? Why don't you calculate the delay to 01:01 and do one big sleep instead of looping?

I think there's several examples of similar problems in these forums. Did you consider searching in here?

1 Like

Hi, thanks for the reply. The system I use is a sun Solaris. The break part was just away of sitting there and waiting for a job to finish, this starts the same time the other job does (8pm) once the other job finishes It creates the trigger file. I get this to wait for it and kick off. I'm doing this the automate several tasks. Someone tried to do it via the cron and it went horribly wrong. You're right about the test, could increase it for sure. I can't put a big delay on it because the job I wait for can take a short time or a long time. So I get it to wait.
As for searching, I love to look around to learn, as I said I'm very new at this scripting lark :stuck_out_tongue: I posted here to see others approach. I will continue to look and learn what i can as my approach can be very converluted. Any input positive or negative is help because I can hopefully learn from it.

Some optimizations

checktime=`ls -l /tmp/TRIGGER | awk '{ print $8 }' | sed 's/://'`

or if you check very often

checktime=`date +%H%M`

Schedule the command for 01:00

echo "Bstats1.sh 1" | at 01:00

An exercise with the at command:

man at
at -l
at -r ...
1 Like

Thanks for the comments. Will look for the "at" command for sure, not seen that one before.