Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 10, 2012, 01:51 PM   #1
CodeBreaker
macrumors 6502
 
Join Date: Nov 2010
Location: Sea of Tranquility
Creating a simple Hello World launch daemon

I'm having a hard time creating a simple hello world launch daemon. It is launched every 10 seconds and just logs "hello world" to the console. So I just compiled the Command Tool (Foundation) template of Xcode. Then I moved the product to /tmp/ and created a plist for launchd:

Code:
<?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 it into launchd using launchctl.
Launchd starts the process every 10 seconds, but it does not log "hello world". Also I keep getting this in the console:
Code:
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?
__________________
.

Last edited by CodeBreaker; Mar 11, 2012 at 01:00 AM.
CodeBreaker is offline   0 Reply With Quote
Old Mar 10, 2012, 02:31 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
Post the source code for helloDaemon.

Have you read TN2083? It's indispensible:
https://developer.apple.com/library/...83/_index.html

Have you tried running your program as a LaunchAgent?
Is there a specific reason you're not running it as a LaunchAgent?


You have two separable issues:
1. The code in helloDaemon.
2. The launchd plist.

I recommend separating them. I also recommend not relying on system console output. Instead, append any output to a specific file.

For example, to append output to a specific file in /tmp, the following shell command would work:
Code:
echo "Hello daemon" >>/tmp/hello.txt
Since you can't use shell redirection operators in a launchd plist, you must tell launchd that bash is the program to run, then it will interpret the command line. Bash has a -c option which interprets the remaining args as the commands to run. So:
Code:
<array>
	<string>/bin/bash</string>
	<string>-c</string>
	<string>echo "Hello daemon" >>/tmp/hello.txt</string>
</array>
Or look up the launchd plist keys StandardOutPath and StandardErrorPath.
chown33 is online now   0 Reply With Quote
Old Mar 10, 2012, 11:45 PM   #3
CodeBreaker
Thread Starter
macrumors 6502
 
Join Date: Nov 2010
Location: Sea of Tranquility
Quote:
Originally Posted by chown33 View Post
Post the source code for helloDaemon.

Have you read TN2083? It's indispensible:
https://developer.apple.com/library/...83/_index.html

Have you tried running your program as a LaunchAgent?
Is there a specific reason you're not running it as a LaunchAgent?


You have two separable issues:
1. The code in helloDaemon.
2. The launchd plist.

I recommend separating them. I also recommend not relying on system console output. Instead, append any output to a specific file.

For example, to append output to a specific file in /tmp, the following shell command would work:
Code:
echo "Hello daemon" >>/tmp/hello.txt
Since you can't use shell redirection operators in a launchd plist, you must tell launchd that bash is the program to run, then it will interpret the command line. Bash has a -c option which interprets the remaining args as the commands to run. So:
Code:
<array>
	<string>/bin/bash</string>
	<string>-c</string>
	<string>echo "Hello daemon" >>/tmp/hello.txt</string>
</array>
Or look up the launchd plist keys StandardOutPath and StandardErrorPath.
I started off this project after reading Apple's TN and the launchd.plist man page. My app should be a daemon because i need it to be always running in the background regardless of who logs in to the computer. I will be running it as root. The source for my app is nothing special, it is just Apple's code. There is only one class (main.m), linked against the Foundation framework (which is daemon safe). Here's the content of main.m:
Code:
#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        // insert code here...
        NSLog(@"Hello, World!");
        
    }
    return 0;
}
I tried you shell code, which works perfectly.

Thanks
__________________
.
CodeBreaker is offline   0 Reply With Quote
Old Mar 11, 2012, 12:59 AM   #4
CodeBreaker
Thread Starter
macrumors 6502
 
Join Date: Nov 2010
Location: Sea of Tranquility
Ok, so apparently NSLogs don't work. I directed the output to a file and it worked. Thanks chown33.
__________________
.
CodeBreaker is offline   0 Reply With Quote
Old Mar 11, 2012, 01:02 AM   #5
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by CodeBreaker View Post
My app should be a daemon because i need it to be always running in the background regardless of who logs in to the computer. I will be running it as root.
These do not disqualify it from running as a launch agent. There is a system-wide launch agents directory in /Library. They are run as root by default.
chown33 is online now   0 Reply With Quote
Old Mar 11, 2012, 06:45 AM   #6
CodeBreaker
Thread Starter
macrumors 6502
 
Join Date: Nov 2010
Location: Sea of Tranquility
Quote:
Originally Posted by chown33 View Post
These do not disqualify it from running as a launch agent. There is a system-wide launch agents directory in /Library. They are run as root by default.
Correct me if I am wrong. I believe Launch Agents are run on behalf of a user and hence need a user to be logged in to the OS, whereas Launch Daemons can be run even if no one is logged into the OS.
__________________
.
CodeBreaker is offline   0 Reply With Quote
Old Mar 11, 2012, 01:15 PM   #7
KnightWRX
macrumors Pentium
 
KnightWRX's Avatar
 
Join Date: Jan 2009
Location: Quebec, Canada
Quote:
Originally Posted by CodeBreaker View Post
Ok, so apparently NSLogs don't work.
Why not use the good old syslog() call ?

Code:
SYSLOG(3)                BSD Library Functions Manual                SYSLOG(3)

NAME
     closelog, openlog, setlogmask, syslog, vsyslog -- control system log

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <syslog.h>
...
void
     syslog(int priority, const char *message, ...);
That way you don't have to worry about rotating your log file/configuring its location and other details the system syslog daemon takes care of.
__________________
"What you leave behind is not what is engraved in stone monuments, but what is woven into the lives of others."
-- Pericles
KnightWRX is offline   0 Reply With Quote
Old Mar 11, 2012, 01:55 PM   #8
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by CodeBreaker View Post
Correct me if I am wrong. I believe Launch Agents are run on behalf of a user and hence need a user to be logged in to the OS, whereas Launch Daemons can be run even if no one is logged into the OS.
That's basically right, but see the man pages for launchd.plist and launchctl. Look at the plist key LimitLoadToSessionTypes, then at the -S option to launchctl which lists the session types. The LoginWindow session type will run a launch agent at the login window. For example, I have a wacom graphics tablet whose LaunchAgents plist file contains Aqua and LoginWindow session types. This means it runs even when no one is logged in. The file is owned by root, and is unwritable to others.

I have not experimented with the other session types.
chown33 is online now   0 Reply With Quote
Old Mar 16, 2012, 11:14 AM   #9
CodeBreaker
Thread Starter
macrumors 6502
 
Join Date: Nov 2010
Location: Sea of Tranquility
Quote:
Originally Posted by KnightWRX View Post
Why not use the good old syslog() call ?

Code:
SYSLOG(3)                BSD Library Functions Manual                SYSLOG(3)

NAME
     closelog, openlog, setlogmask, syslog, vsyslog -- control system log

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <syslog.h>
...
void
     syslog(int priority, const char *message, ...);
That way you don't have to worry about rotating your log file/configuring its location and other details the system syslog daemon takes care of.
Thanks for the tip. I basically needed logs to just find out if my code was working.

Quote:
Originally Posted by chown33 View Post
That's basically right, but see the man pages for launchd.plist and launchctl. Look at the plist key LimitLoadToSessionTypes, then at the -S option to launchctl which lists the session types. The LoginWindow session type will run a launch agent at the login window. For example, I have a wacom graphics tablet whose LaunchAgents plist file contains Aqua and LoginWindow session types. This means it runs even when no one is logged in. The file is owned by root, and is unwritable to others.

I have not experimented with the other session types.
Login window seems just the right time for my program to launch. Thanks for the info.
__________________
.
CodeBreaker is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Microsoft's 'Age of Empires: World Domination' to Launch on iOS This Summer MacRumors iOS Blog Discussion 35 Apr 16, 2014 10:19 AM
Creating a Launch Deamon for Portsentry ACagliano Mac Programming 5 Feb 1, 2013 10:07 AM
iSight not recognised in iMovie, launch Daemon .plist missing, won't rebuild. Monsoonmalabard OS X 10.8 Mountain Lion 1 Dec 13, 2012 07:22 AM
Need Assitance in creating a simple Cocoa OS X application lilabila Mac Programming 2 Aug 18, 2012 12:52 PM

Forum Jump

All times are GMT -5. The time now is 01:17 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC