Hi all -- I am trying to design a perl script that triggers data collection and logging every X seconds. The problem is that the collection process (reading data from an external instrument via a GPIB port) takes an imprecise amount of time; on a 60-second cycle, I can estimate the time the instrument will take to within two or three seconds, but not more closely than that.

So something as simple as:

while (1) { get_data_taking_exactly_55_seconds; sleep(5); write_log_file; }

doesn't cut it; my log entries won't be properly aligned because the data collection function may take a bit more or less than 55 seconds.

What I want to do, I think, is along the lines of:

while (1) { set timer; get_data_taking_55_seconds_give_or_take_a_few; sleep_until_timer_says_60_seconds; write_log_file; }

It seems like using the alarm function to trigger the reading process on receipt of SIGALRM should let me do this but I haven't figured out how to make it work reliably; in particular, I can't figure out what kind of loop structure I need to keep the program alive.

I'd sure appreciate any help on how to implement a "sleep until alarm" capability in my program.

Thanks!


In reply to cron-like timing within perl script? by n8ur

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.