1832938 Members
2640 Online
110048 Solutions
New Discussion

shell sleep question.

 
brian_31
Super Advisor

shell sleep question.

Team:

I have a very small shell program that does this:

while `sleep 86400`
do
..
..
echo "Something"
...
...
done

(86400 is one day).. The program does not seem to wake up after 1 day, and process what I want it to do everyday.

If I change 86400 to some smaller number, it seems to work fine. Could anyone tell why something like this happens?

Thanks

Brian.
10 REPLIES 10
Fred Ruffet
Honored Contributor

Re: shell sleep question.

Your script as you want it should be :
while true
do
...
echo "Something"
...
sleep 86400
done

But you may seriously consider using crontab.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Sundar_7
Honored Contributor

Re: shell sleep question.

Fred is right on. To explain why it is not working when you use 86400 with sleep.

while `sleep 86400`

while expects a condition that evaluates to "true" or "false".

WHILE executes the sleep 86400 and waits for the completion.

sleep command completes after one day and returns the exit code (should be 0) to while. Now WHILE will go ahead and execute the body.

Clearly, this is not good scripting but I cannot think of a reason why this was not working for you. It should have echoed "Something" after one day :-).

Also understand, the above while loop is infinite. There is no exit condition. Not a very good scripting practise either.
Learn What to do ,How to do and more importantly When to do ?
John Kittel
Trusted Contributor

Re: shell sleep question.

From the man page,

sleep exits with one of the following values:

0 The execution was successfully suspended for time seconds, or a SIGALRM signal was received.

>0 If the time operand is missing, is not a decimal integer, is negative, or is greater than UINT_MAX, sleep returns with exit status 2.

brian_31
Super Advisor

Re: shell sleep question.

so..could someone tell me why it works if i change to a lesser value?

Thanks

Brian
Rodney Hills
Honored Contributor

Re: shell sleep question.

The man page says-

If the time operand is missing, is not a decimal integer, is negative, or is greater than UINT_MAX, sleep returns with exit status 2.

Could 86400 be greater than UINT_MAX?

-- Rod Hills
There be dragons...
Jeff Schussele
Honored Contributor

Re: shell sleep question.

Hi Rodney,

According to /usr/include/limits.h UINT_MAX is either 4,294,967,295 ...or... 37,777,777,777 depending on how STDC is defined.
But these are values *IF* UINT_MAX is undefined.
I suspect that sleep may define it.

At what value does the behavior begin - i.e. does it not happen at 86399? If so I'd suspect that UINT_MAX is indeed being set to 86400.

My $0.02,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Rodney Hills
Honored Contributor

Re: shell sleep question.

Could the process be terminated by another process that is running? Like a backup script that cleans unnecessay processes?

-- Rod Hills
There be dragons...
Muthukumar_5
Honored Contributor

Re: shell sleep question.

If you are executing scripts liek this, it will be running on system on whole day. It is not efficient to do soem jobs there. Try to go to crontab to do easily and effectively there.

# getconf UINT_MAX ( 11.00)
4294967295

It may not be the problem.

Try to check script as,

while true
do

sleep 86400
if [[ $? -eq 2 ]]
then
echo "`date`problem with UINT_MAX `getconf UINT_MAX`"
else
echo "`date`Sleep returns $?"
fi

done

Redirect to another log file to analyse results later.
Easy to suggest when don't know about the problem!
John Palmer
Honored Contributor

Re: shell sleep question.

Hi Brian,

How are you executing the script?

If all you do is run it as a background job (script &), when you log out your session, the script will receive a SIGHUP and terminate.

To prevent this, there are several possibilities:-
1. Run it as a batch job (batch or at now)
2. Start it with nohup.
3. Include 'trap "" 1' in the script.

Regards,
John
Chang_6
Regular Advisor

Re: shell sleep question.

Brian,

When you use while `sleep 86400`, you are first oputting it to sleep. So, there is no way you could see what is happening. Whereas, if you say sleep 180, you will notice it works after 3 minutes.