Im trying to pause my script for 23:59 to 6am but it is throwing error:
Code below:
#!/system/bin/sh
while true
do
TIME=$(date '+%H%M%S')
if [ $TIME -ge 235900 ] -a [ $TIME -le 060000 ]
then*
echo "time between noon and morning"*
echo sleep time $TIME
sleep 3
else
then
echo "time all others"
fi
done
'[' 100652 -ge 235900 ']' -a '[' 100652 -le 060000 ']'
bash: [: too many arguments
'then*'
then*: command not found
, and the sequence
else
then
also doesn't make any sense.
By commenting that using decent code structuring like indenting and block building helps you and others read, understand and maintain your code?
With the question what you want to achieve with that small code snippet? It doesn't seem to make sense to have an infinite loop running all day long, wasting enormous amounts of resources for doing nothing? There are dedicated tools for dealing with time dependent tasks, like cron or at .
With the remark that date / time arithmetics is extremely complex by itself and becomes more difficult if crossing midnight?
Here is a sample code doing what I guess you want but starting at midnight. It sleep s the necessary amount of seconds until 6:00h in the morning if run between midnight and 6:00h. At all other times, it sleep s a minute and repeats the test. Feel free to adapt to your lower limit of one minute before midnight:
while true
do TIME=$(date '+%H%M%S')
if [ $TIME -ge 000000 -a $TIME -le 060000 ]
then echo "time between noon and morning"*
echo sleep $(( $(printf "06:00\nnow\n" | date -f- +%s-) 0 ))
sleep $(( $(printf "06:00\nnow\n" | date -f- +%s-) 0 ))
else echo "time all others"
sleep 60
fi
done
I'm sure there will be better solutions if you explain exactly what you're after in a larger perspective.
Hi RudiC,
Thanks for the reply. I have a script named termination.sh where I want this to run keep running only in the time specified time i.,e 06 to 23:59 but I don't want this to run/execute again and again,script once run will do the job for me.The only thing I want is to make termination.sh script to sleep for 00:00 to 06:00 am. termination.sh script is a java script where I have java code inside the jar.
--- Post updated at 12:02 PM ---
I want my termination script to run only between morning from 06:00am to 00:00.
--- Post updated at 12:10 PM ---
I want to run termination.sh script inside the above given script where it should be running only in between 06 to 23:59.