Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a simple Hello World tool that runs as a daemon

I built the Command Line tool (Foundation) template in Xcode. It just logs "Hello World" to the console.There is only one class in it main.m. Here's the code:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

{

   @autoreleasepool {

       // insert code here...
       NSLog(@"Hello, World!");

   }
   return 0;

}

Now I want to run it as a daemon and log "Hello World" to the console every 10 seconds. So I moved the product/binary to /tmp on my Mac. I created the following plist for launchd:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>helloDaemon</string>
    <key>ProgramArguments</key>
    <array>
        <string>/tmp/helloDaemon</string>
    </array>
    <key>StartInterval</key>
    <integer>10</integer>
</dict>
</plist>

I loaded the plist using launchctl, but I do not see any "Hello World"s in the console. Instead, I get this:

11/03/2012 00:55:35.141 com.apple.launchd: (helloDaemon) Throttling respawn: Will start in 1 seconds
11/03/2012 00:55:45.141 com.apple.launchd: (helloDaemon) Throttling respawn: Will start in 2 seconds
11/03/2012 00:55:55.140 com.apple.launchd: (helloDaemon) Throttling respawn: Will start in 3 seconds

So what's going wrong?

like image 293
CodeBreaker Avatar asked Nov 26 '25 10:11

CodeBreaker


1 Answers

Just add to your launchd plist

<key>StandardOutPath</key>
<string>/yourpath/sample.log</string>

You can tail -f it afterwards.

More info is here: http://developer.apple.com/library/mac/technotes/tn2083/_index.html#//apple_ref/doc/uid/DTS10003794-CH1-SUBSECTION39

like image 51
bioffe Avatar answered Nov 29 '25 01:11

bioffe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!