How does StartInterval key affects the started daemon - ios

I have a application run as a daemon. I put the application plist under under /Library/LaunchDaemons on iOS 8 device, and launch it by executing the command
launchctl load /Library/LaunchDaemons/com.mycompany.testapp.plist
In my laumchd plist Note that the application is running as a daemon by executing the command
I would like to make this application restart only when it is crash or is killed. If I intentionally exit it with code 0, I don't want it to restart. I've tried the below configuration. This does work on iOS 7, but do NOT work on iOS 8.
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<true/>
</dict>
Thus I tried adding another key StartInterval which's set as 10.
<key>StartInterval</key>
<integer>10</integer>
I tested this scenario by exit with code 0 and by kill my application with command kill -9 [PID]. This key makes my application to start again after 10 seconds after being killled. However, I have a concern about the result of this key while my application is running.
Does this key have an affect on the started and running application. I've monitored the log, seem StartInterval key do nothing with the running daemon. However, I'm quite not sure about it. Could you explain more information about it? Thank you very much.

According to this link http://pitaya.ch/documentation/iOS8_0APIDiffs.html all traces to launchd in the headers have disappeared. (search for /usr/include/launch.h, so you see it was removed)
Seems because the moved to new libxpc as base for launchd (see last slide in http://technologeeks.com/docs/launchd.pdf).
Seems like they changed launchd behaviour in iOS 8. Of Apple's own LaunchDaemons (located under /System/Library/LaunchDaemons/) some also have a KeepAlive-property in Dictionary format. If you kill them, they are respawned by launchd instantly.
But after some testing the only way to have KeepAlive-property working, is to add either add LaunchEvents (see slide 21ff in http://technologeeks.com/docs/launchd.pdf) and/or MachServices to the plist or to change the KeepAlive-property to:
<key>KeepAlive</key>
<true/>
But then there is NO way to kill your deamon without launchd respawning it, aside from executing launchctl unload /Library/LaunchDaemons/com.mycompany.testapp.plist.
My working plist:
<?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>Program</key>
<string>/path/to/your/daemon</string>
<key>RunAtLoad</key>
<true/>
<key>Label</key>
<string>com.mycompany.testapp</string>
<key>EnablePressuredExit</key>
<false/>
<key>UserName</key>
<string>root</string>
<key>JetsamProperties</key>
<dict>
<key>JetsamPriority</key>
<integer>-49</integer>
<key>JetsamMemoryLimit</key>
<integer>10000</integer>
</dict>
<key>POSIXSpawnType</key>
<string>Adaptive</string>
<key>Disabled</key>
<false/>
<key>ThrottleInterval</key>
<integer>5</integer>
<key>KeepAlive</key>
<dict>
<key>SuccessfulExit</key>
<false/>
<key>Crashed</key>
<true/>
</dict>
<key>MachServices</key>
<dict>
<key> com.mycompany.testapp.someMachService</key>
<true/>
</dict>
<key>EnableTransactions</key>
<false/>
<key>LaunchEvents</key>
<dict>
<key>com.apple.notifyd.matching</key>
<dict>
<key>SignificantTimeChangeNotification</key>
<dict>
<key>Notification</key>
<string>SignificantTimeChangeNotification</string>
</dict>
<key>SomeMoreNotifications</key>
[...]
</dict>
</dict>
</dict>
</plist>
About the StartInterval-property: Even Apple has this property in addition to the KeepAlive-property for some important daemons (e.g. com.apple.locationd) and they seem running just fine. So I don't think you have to worry...

Use false with SuccessfulExit key.
SuccessfulExit
If true, the job will be restarted as long as the program exits and with an exit status of zero.
If false, the job will be restarted in the inverse condition. This key implies that "RunAtLoad"
is set to true, since the job needs to run at least once before we can get an exit status.

Related

LaunchDaemons is giving error 127

i have a problem when i run LaunchDaemons this is my .plist:
<?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>com.startup</string>
<key>ProgramArguments</key>
<array>
<string>/Users/myuser/project/workspace/tomcat/run/tomcat-
exp.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WatchPaths</key>
<array>
<string>/Users/myuser/project/workspace/tomcat/log/exp.out</string>
</array>
</dict>
</plist>
and this is the error am getting, i have been trying to solve this issue for hours but no suceess :(
when i checked system log this is what i found:
16239 Dec 28 21:36:29 myyser MRT[1481]: Error: SMJobRemove: The
operation couldn’t be completed. (CFErrorDomainLaunchd error 2.)
16240 Dec 28 21:36:29 myuser com.apple.xpc.launchd[1]
(com.apple.mrt[1481]): Service exited with abnormal code: 2
16241 Dec 28 21:36:29 myuser com.apple.xpc.launchd[1] (com.apple.mrt):
Service only ran for 0 seconds. Pushing respawn out by 10 seco
nds.
Your plist looks running shell script. Exit code 127 is reported when command is not found, usually $PATH is not correct or just by typo.
Other than that, your plist looks ok though I have only used OnDemand, and I haven't run script.
What I usually do to debug daemons is to run them from command line
# /bin/launchctl load /Library/LaunchDaemons/mydaemon.plist
and see debug messages by fprintf on shell. You can redirect your stdout by adding
<key>StandardOutPath</key>
<string>/var/log/mylog.log</string>
to your .plist. Does your daemon work w/o any issue when started by command line manually ?
See this link for exit codes of scripts of bash.
We also had the same problem with error 127. However in our case we tried to use a symbolic inside a plist file running a peerJs server which was causing the problem discussed here
Avoid using a symbolic link inside a launch daemon file and use real path to a specific file

Relative Path in launchd plist

I am currently using a plist to run a shell script.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.name.set</string>
<key>Program</key>
<string>/Users/username_here/Desktop/simple.sh</string>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>5</integer>
<key>StandardErrorPath</key>
<string>/tmp/com.name.example.stderr</string>
<key>StandardOutPath</key>
<string>/tmp/com.name.example.stdout</string>
</dict>
</plist>
This works! But when I change the program name to be
<string>/Desktop/simple.sh</string>
it doesn't run the script. also ~/Desktop/simple.sh does not work.
Is there a way to run the script without knowing the username and using an absolute path?
I am also getting this error message when I tail launchd.
com.apple.xpc.launchd[1] (com.name.example[8178]): Service could not initialize: 14F27: xpcproxy + 13421 [1402][AD0301C4-D364-31CE-8BA7-B5DBECE64D0A]: 0x2
Thanks!
Using a shell as arg0 and giving it a path relative to the user's home folder works for me:
<key>ProgramArguments</key>
<array>
<string>zsh</string>
<string>-c</string>
<string>~/CLI/scripts/list_open_jira_tickets --skip=5297 > ~/CLI/tmp/open_jira_tickets.txt</string>
</array>
If daemon is run as a per-user agent (installed in ~/Library/LaunchAgent) you can use a period get the relative path. Which would be the home folder (~/)
So you can do this:
<key>Program</key>
<string>./Desktop/simple.sh</string>
It might be nicer to save your program next to your plist:
./Library/LaunchAgents/simple.sh

Possible to use self-signed SSL certificate with itms-services

I'm trying to install a beta app to an iOS device and am hosting the manifest.plist and IPA file on HTTPS with a self-signed certificate. When I hit the itms-services:// link, I get an error of "Cannot connect to <ip address>".
itms-services link:
itms-services://?action=download-manifest&url=https://10.0.1.2:1338/ipa/manifest.plist
plist:
<?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>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>https://10.0.1.2:1338/ipa/test.ipa</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>com.company.test</string>
<key>bundle-version</key>
<string>1.0</string>
<key>kind</key>
<string>software</string>
<key>title</key>
<string>test</string>
</dict>
</dict>
</array>
</dict>
</plist>
Try adding your certificate to the trusted ones (probably, easiest thing to do is to mail a cer file to device and open it from standard mail app to install).
Another thing you need to make sure is that your server supports TLS 1.2 and Forward Secrecy.
You can use nscurl --ats-diagnostics https://example.com/app.ipa to check if everything done right.
Its possible:
Create the certificate file (export as DER encode X.509)
Access file from device via HTTP (like the manifest.plist in your example)
Agree to install it in device
Turn it on: https://support.apple.com/en-au/HT204477

Starting YAWS using launchd under Mac OS X Yosemite

I am trying to setup Yaws (installed via Homebrew) to start automatically under OS X Yosemite using the following PLIST:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>ca.seidlitz.yaws</string>
<key>ProgramArguments</key>
<array>
<string>sh</string>
<string>-c</string>
<string>/usr/local/Cellar/yaws/1.98/bin/yaws --daemon --conf /usr/local/Cellar/yaws/1.98/etc/yaws/yaws.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/usr/local/opt/yaws</string>
<key>StandardOutPath</key>
<string>/tmp/yaws.log</string>
<key>StandardErrorPath</key>
<string>/tmp/yaws_err.log</string>
</dict>
</plist>
I tried to load this plist using sudo launchctl load /Library/LaunchDaemons/ca.seidlitz.yaws.plist
but with no luck. I don't get any error in /var/system.log, while the /tmp/yaws.log contains this error: "1> *** Terminating erlang (nonode#nohost)"
I also tried running it as daemon and in interactive mode. Daemon doesn't generate any errors in the log file but Yaws is not running.
Can anyone spot any issues with the plist?
You should make the /usr/local/Cellar/yaws/1.98/bin/yaws script executable if it isn't already, get rid of the sh -c invocation, and properly declare the yaws script and its arguments as separate strings, all like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>ca.seidlitz.yaws</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/Cellar/yaws/1.98/bin/yaws</string>
<string>--daemon</string>
<string>--conf</string>
<string>/usr/local/Cellar/yaws/1.98/etc/yaws/yaws.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/tmp</string>
<key>StandardOutPath</key>
<string>/tmp/yaws.log</string>
<key>StandardErrorPath</key>
<string>/tmp/yaws_err.log</string>
</dict>
</plist>

ios-sim in Jenkins Process leaked file descriptors

I am getting a file leak descriptors in Jenkins when trying to launch ios-sim using the shell. Something I should mention is that I changed the user of Jenkins to my user away from the initially installed "/Users/Shared/Jenkins" location. Here's the message
./ios-sim launch /Users/me/Desktop/tmp/iOS.app --sdk 6.0 --family iphone
Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more
The link states it may be due to not being able to detect the EOF. If so how can point Jenkins to write to "stdin/stdout/stderr"?
Is it a matter of chowning some files that I may of missed when I moved the "/User/Shared/Jenkins/Home" location to /Users/me/Jenkins/Home and the "/Applications/Jenkins/jenkins.war" file to "/User/me/Applications/jenkins.war" ?
In addition to those moves I updated the org.jenkins-ci.plist as follows:
<plist version="1.0">
<dict>
<key>StandardOutPath</key>
<string>/var/log/jenkins/jenkins.log</string>
<key>StandardErrorPath</key>
<string>/var/log/jenkins/jenkins.log</string>
<key>EnvironmentVariables</key>
<dict>
<key>JENKINS_HOME</key>
<string>/Users/me/Jenkins/Home</string>
</dict>
<key>GroupName</key>
<string>staff</string>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>org.jenkins-ci</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/Library/Application Support/Jenkins/jenkins-runner.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>me</string>
<key>SessionCreate</key>
<true />
</dict>
</plist>
I'm using Jenkins version 1.499
And here is the jenkins.log error:
WARNING: Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more information
java.lang.Exception
at hudson.Proc$LocalProc.join(Proc.java:329)
at hudson.Launcher$ProcStarter.join(Launcher.java:360)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:91)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:60)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:19)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:810)
at hudson.model.Build$BuildExecution.build(Build.java:199)
at hudson.model.Build$BuildExecution.doRun(Build.java:160)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:592)
at hudson.model.Run.execute(Run.java:1543)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
at hudson.model.ResourceController.execute(ResourceController.java:88)
at hudson.model.Executor.run(Executor.java:236)
"Insane in the membrane" why is it advertised everywhere to stop start and stop jenkins using:
sudo launchctl unload /Library/LaunchDaemons/org.jenkins-ci.plist
sudo launchctl load /Library/LaunchDaemons/org.jenkins-ci.plist
???? Who wants to edit the jenkins-runner.sh or the add values to the .plist file???? boooo
The secret is in the secret user jenkins created upon install in the "/Users/me/.jenkins" location.
You need to own all the processes and in order to that just locate the jenkins.war file and launch it like so:
nohup java -jar ~/.jenkins/jenkins.war --httpPort=8080
Suddenly everything starts working under the user on your company machine!
enjoy

Resources