Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:15:53+00:00 2026-05-28T19:15:53+00:00

I have a daemon that launchd runs at system boot (OS X). I need

  • 0

I have a daemon that launchd runs at system boot (OS X). I need to delay startup of my daemon by 3-5 seconds, yet the following code executes instantly at boot, but delays properly well after boot:

#include <unistd.h>
...
printf("Before delay\n");
unsigned int delay = 3000000;
while( (delay=usleep(delay)) > 0)
{
   ;
}
printf("After delay\n");

If I run it by hand after the system has started, it delays correctly. If I let launchd start it at boot the console log shows that there is no delay between Before delay and After delay – they are executed in the same second.

If I could get launchd to execute my daemon after a delay after boot that would be fine as well, but my reading suggests that this isn’t possible (perhaps I’m wrong?).

Otherwise, I need to understand why usleep isn’t working, and what I can do to fix it, or what delay I might be able to use instead that works that early in the boot process.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:15:54+00:00Added an answer on May 28, 2026 at 7:15 pm

    First things first. Put in some extra code to also print out the current time, rather than relying on launchd to do it.

    It’s possible that the different flushing behaviour for standard output may be coming into play.

    If standard output can be determined to be an interactive device (such as running it from the command line), it is line buffered – you’ll get the "before" line flushed before the delay.

    Otherwise, it’s fully buffered so the flush may not happen until the program exits (or you reach the buffer size of (for example) 4K. That means that launchd may see the lines come out together, both after the delay.

    Getting the C code to timestamp the lines will tell you if ths is the problems, something like:

    #include <stdio.h>
    #include <time.h>
    #include <unistd.h>
    
    int main (void) {
        printf("%d: Before delay\n", time(0));
        unsigned int delay = 3000000;
        while( (delay=usleep(delay)) > 0);
        printf("%d: After delay\n", time(0));
    
        return 0;
    }
    

    To see why the buffering may be a problem, consider running that program above as follows:

    pax> ./testprog | while read; do echo $(date): $REPLY; done
    Tue Jan 31 12:59:24 WAST 2012: 1327985961: Before delay
    Tue Jan 31 12:59:24 WAST 2012: 1327985964: After delay
    

    You can see that, because the buffering causes both lines to appear to the while loop when the program exits, they get the same timestamp of 12:59:24 despite the fact they were generated three seconds apart within the program.

    In fact, if you change it as follows:

    pax> ./testprog | while read; do echo $(date) $REPLY; sleep 10 ; done
    Tue Jan 31 13:03:17 WAST 2012 1327986194: Before delay
    Tue Jan 31 13:03:27 WAST 2012 1327986197: After delay
    

    you can see the time seen by the "surrounding" program (the while loop or, in your case, launchd) is totally disconnected from the program itself).

    Secondly, usleep is a function that can fail! And it can fail by returning -1, which is very much not greater than zero.

    That means, if it fails, your delay will be effectively nothing.

    The Single UNIX Specification states, for usleep:

    On successful completion, usleep() returns 0. Otherwise, it returns -1 and sets errno to indicate the error.

    The usleep() function may fail if: [EINVAL]: The time interval specified 1,000,000 or more microseconds.

    That’s certainly the case with your code although it would be hard to explain why it works after boot and not before.

    Interestingly, the Mac OSX docs don’t list EINVAL but they do allow for EINTR if the sleep is interrupted externally. So again, something you should check.

    You can check those possibilities with something like:

    #include <stdio.h>
    #include <time.h>
    #include <errno.h>
    #include <unistd.h>
    
    int main (void) {
        printf("%d: Before delay\n", time(0));
        unsigned int delay = 3000000;
        while( (delay=usleep(delay)) > 0);
        printf("%d: After delay\n", time(0));
        printf("Delay became %d, errno is %d\n", delay, errno);
    }
    

    One other thing I’ve just noticed, from your code you seem to be assuming that usleep returns the number of microseconds unslept (remaining) and you loop until it’s all done, but that behaviour is not borne out by the man pages.

    I know that nanosleep does this (by updating the passed structure to contain the remaining time rather than returning it) but usleep only returns 0 or -1.

    The sleep function acts in that manner, returning the number of seconds yet to go. Perhaps you might look into using that function instead, if possible.

    In any case, I would still run that (last) code segment above just so you can ascertain what the actual problem is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small daemon that I'm writing in C and I need a
I need to write a daemon that supposed to have one TCP socket and
I have a launchd daemon that every so often uploads some data via a
I have a daemon that runs constantly which fills up the log file(development.log or
I need a daemon that turns RSS into email. It should have an API
I have to do the following: I have a Daemon set up that every
I am going to have a daemon that will run on a FreeBSD server,
So I have a daemon running on a Linux system, and I want to
I have a simple Perl script which runs as a Linux daemon using an
I have a daemon that I'm starting along with the server using an initializer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.