I wanted to write a simple one-liner to send keep-alive packets over an ssh connection that would otherwise time out and disconnect me. I figured the simple solution is that when I wasn't actively using the terminal, I could run my script to make it "do something"*.
So I did:
bash-2.02$ perl -e '{warn scalar localtime,$/;sleep 60;redo}' Wed Mar 21 10:27:12 2001 Wed Mar 21 10:28:12 2001 Wed Mar 21 10:29:12 2001 ^C
Well, I looked at that and said to my self, "That would look nicer if the seconds field was just 00." So I tried:
bash-2.02$ perl -e 'while(time%60){}{warn scalar localtime,$/;sleep 60 +;redo}' Wed Mar 21 10:39:00 2001 Wed Mar 21 10:39:59 2001 Wed Mar 21 10:40:59 2001 Wed Mar 21 10:41:59 2001 ^C
That through me for a bit of a loop, and I thought about it and remembered that sleep is imprecise.
From Camel2: On some systems, the function sleeps till the "top of the second"
Ok, I can handle that, but then the "top of the second" ought to be 60-61 seconds, right? Nope.
From sleep: On some older systems, it may sleep up to a full second less than what you requested
Well, I can work with that, so after playing with select, I landed on:
bash-2.02$ perl -e '{while(time%60){}warn scalar localtime,$/;sleep 59 +;redo}' Wed Mar 21 10:58:00 2001 Wed Mar 21 10:59:00 2001 Wed Mar 21 11:00:00 2001 Wed Mar 21 11:01:00 2001 ^C
But what I don't understand, and perhaps someone can enlighten me, is why "top of the second" would round down like that, instead of up. Either I don't understand the phrase, or on some older systems it is just a different behaviour. The system, by the way, is running SunOS 5.7. Also, why didn't it drift before I added the initialization? I thought that maybe the initialization was putting it on the edge, so I added a select statement to pause it for an additional tenth of a second, and ten minutes later it had drifted off again. This leads me to think that the system clock it-self might be at fault. Ideas? Insights?
*Why would you want keep alive packets? Why would a system time you out? Well actually, I'm behind a firewall that kills any tcp session where no data is passed for a couple of minutes (ie, the sequence numbers don't change). Not nice when you go to ask someone a question and come back to find that you have been disconnect. Technically, any amount of data would prevent the disconnect, but the time was just more esthetically pleasing.

In reply to Top of the second? by Adam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.