1

I am looking to install a privileged helper tool using SMJobBless.
My plist only contains the MachServices key and RunAtLoad key. The RunAtLoad is set to true as I want to be launched automatically after install.

Now, the problem is that once the helper tool is installed and launched and it completes its job successfully - it gets relaunched and this keeps happening again and again.

I have tried various things to stop it from launching again but in vain:
1. Tried to add KeepAlive to false in the plist.
2. Tried to add KeepAlive as a dictionary with the key SuccessfulExit to false.
3. Tried to make my app sleep for occult times before quitting like sleeping for 5 minutes, 10 minutes etc. but it still gets launched again.

I have basically run out of ideas, I want it to launch only when I ask and not automatically and that too again and again.

Please help. Thank you very much.


OS: Mac OS X 10.8.4 12E55
Xcode: 4.6.2 (4H1003)

dDarkLORD
  • 624
  • 7
  • 25
  • 1
    Does your workflow support using the key LaunchOnlyOnce ? Also are you sure that for your second case, the return code of program is zero, else it will be restarted. – sraok Oct 21 '13 at 05:59
  • No, my workflow does not support it. I want to launch it on multiple occasions without needing a machine reboot. But I tried it anyway and it still did not work. Yes, for the second case, I always return zero only. – dDarkLORD Oct 21 '13 at 06:12
  • You probably need to ask this on http://apple.stackexchange.com – trojanfoe Oct 21 '13 at 06:23
  • Thanks @trojanfoe , I have asked it there as well now... – dDarkLORD Oct 21 '13 at 08:42
  • @dDarkLORD : did you ever find the answer to the problem ? I have exactly the same thing happening in my app. I'd be interested to know. [Question](http://stackoverflow.com/questions/33128496/os-x-truly-on-demand-privileged-helper-tool) – Vince Oct 15 '15 at 11:49

1 Answers1

1

Use KeepAlive with PathState key. Create a file when you want to launch your task. Delete that file before returning zero from your helper tool.(Remove RunAtLoad key)

PathState Each key in this dictionary is a file-system path. If the value of the key is true, then the job will be kept alive as long as the path exists. If false, the job will be kept alive in the inverse condition. The intent of this feature is that two or more jobs may create semaphores in the file-system namespace.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • 1
    It is not working. Even after I delete the file, it keeps launching. – dDarkLORD Oct 21 '13 at 08:46
  • my plist: ` KeepAlive PathState /tmp/helper.txt Label com.ak.SMJobBlessHelper MachServices com.ak.SMJobBlessHelper.mach ProgramArguments /Library/PrivilegedHelperTools/com.ak.SMJobBlessHelper ` – dDarkLORD Oct 21 '13 at 08:47
  • add some sleep after deleting the file and check. – Parag Bafna Oct 21 '13 at 08:50
  • I added 15 second sleep after deleting and before quitting but still does not work. – dDarkLORD Oct 21 '13 at 08:56
  • 1
    I am using this for my daemon process and its working fine. unload your daemon with -w option then load and check. – Parag Bafna Oct 21 '13 at 09:01
  • Thanks @Parag It worked! - why it does not work automatically? Actually I need it to work programatically. – dDarkLORD Oct 21 '13 at 09:40
  • 1
    What do you mean by programmatically? – Parag Bafna Oct 21 '13 at 09:50
  • 1
    Basically, I cannot call `sudo launchctl unload -w ` from command line as I will not have this privilege on user's machine.I cannot also call from within my caller program since I do not have elevated privileges, so I want it to work without doing the unload/load, maybe, by calling some system call or something from my caller program. – dDarkLORD Oct 21 '13 at 09:53
  • 1
    Little more background: There is a caller program that basically installs this helper tool in the elevated location `/Library/PrivilegedHelperTools`. Now, with the PathState entry this should automatically start/stop with the file path but I cannot call unload/load as this will require command line access on user machine. – dDarkLORD Oct 21 '13 at 09:56
  • call launchctl load at the time of installation. it will work. – Parag Bafna Oct 21 '13 at 09:59
  • 1
    The [SMJobBless](https://developer.apple.com/librarY/mac/samplecode/SMJobBless/Introduction/Intro.html) automatically installs the helper tool at the desired place by prompting user for an authorization dialog. But I (the caller program) does not get those elevated rights so I cannot load/unload it. – dDarkLORD Oct 21 '13 at 10:03
  • I again checked the load/unload will not work without sudo and I (caller program) do not have elevated rights. So, I want it to happen without the load/unload. – dDarkLORD Oct 21 '13 at 10:31
  • Why would one use this hack to get the whole thing working? Sure it works.. But we still don't know what the problem is, and how to make things work properly. – Vince Oct 15 '15 at 11:45
  • @Vince This is not a hack. Real all comments in question and read launchchd documentation – Parag Bafna Oct 15 '15 at 12:20
  • @ParagBafna: I did. So let me call this a workaround the problem instead. The real key to use with `KeepAlive` would be `SuccessfulExit`. The `PathState` key here isn't used as intended it seems. So not a hack for sure... – Vince Oct 15 '15 at 12:25
  • @Vince How to solve "I want to launch it on multiple occasions without needing a machine reboot." ? – Parag Bafna Oct 15 '15 at 12:28
  • That is precisely what I want to achieve too. The original question was why, despite the `KeepAlive` with `SuccessfulExit` keys being set, `launchd` keeps restarting the process every time it quits. The documentation doesn't says the `PathState` key should be used to control the way launchd restart (or not) the process. To me, it remains a (working) hack, but doesn't answer the question. – Vince Oct 15 '15 at 12:31
  • @ParagBafna btw, the `PathState` hack doesn't work for me either. – Vince Oct 15 '15 at 13:40