1832655 Members
3156 Online
110043 Solutions
New Discussion

Scripting question

 
SOLVED
Go to solution
yc_2
Regular Advisor

Scripting question

Hi,

Is it possible to have a timer control in a script within a while true loop to stop it from execution. Cron job will use to start the script.

If so, how should be the syntax like.

i.e.
while (08:00 < time < 19:00)
do
...
sleep 5
done


Thanks in advance,
YC
3 REPLIES 3
RikTytgat
Honored Contributor

Re: Scripting question

Hi,

You can do it, but it is not as simple as your code.

By using the date command with format specifiers, you can easily retrieve the hour and minutes.

HOUR=$(date +"%H")

if [[ ${HOUR} -lt 15 && ${HOUR} -gt 8 ]]
then
# do whatever it is you want to do between 8 and 15
else
# do something else
fi


Hope this helps,
Rik.
RikTytgat
Honored Contributor
Solution

Re: Scripting question

Hi again,

In case of a while construction, this would be:

HOUR=$(date +"%H")

while [[ ${HOUR} -lt 15 && ${HOUR} -gt 8 ]]
do
# do whatever it is you want to do between 8 and 15
HOUR=$(date +"%H")
done



Bye,
Rik.
Dan Hetzel
Honored Contributor

Re: Scripting question

Hi,

Just before your loop, you could spawn a child process consisting of a single sleep
followed with a line like the following
kill -USR1 $PPID.

If your parent program traps the USR1 signal,
it can keep on running as long as the
kill isn't issued by the child and then
do whatever is needed, for example exit from
a loop.

Regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com