0

Actually, I am quite new in Mac OS X launchd property list.

I am planning to deploy a unix daemon program to serve my customers in only their office hours. For example 7:00 AM - 9:00PM.

Is there any possible only use one property list ***.plist file to fulfil it or I need two separated property list files to start/stop the daemon services?

Please advise or any suggestion are welcome!

Plenty appreciation!

Edit:

Yeah. I am planning to start and stop my daemon by using launchd. I tried to use parameter such as:

<key>ProgramArguments</key>
<array>
    <string>myDaemon</string>
    <string>-e</string>
    <string>61200</string>
</array>
<key>StartCalendarInterval</key>
<dict>
    <key>Minute</key>
    <integer>5</integer>
    <key>Hour</key>
    <integer>6</integer>
</dict>

I tried pass -e 61200 as parameter to let the daemon keep running 17 hours and then exit. But unfortunately. This way not work.

cidy.long
  • 417
  • 12
  • 35
  • 1
    Can you add some detail please? Do you mean for `launchd` to start and stop your processes according to the time of day? What parameters in the *plist* file were you planning to use? Or were you thinking of having a second launchd task that merely wakes up each evening and kills the other launchd tasks that were started in the morning? Any reason why the server itself can't decide when to shut down? Surely if it is clever enough to be a server, it is clever enough to start a second thread on startup that kills itself at a specific end time? – Mark Setchell May 09 '15 at 08:55
  • I tried start it at 06:05AM start and keep running 17 hours stop. but seems it not work. – cidy.long May 09 '15 at 10:15

2 Answers2

0

The launchd .plist file can only describe when the daemon should be started, not when it should be stopped or how long it should be allowed to run before being stopped.

Your daemon should exit itself after a given time interval if that's what you want.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • The ability to set a lifetime or quit time in a launchd plist would make an interesting feature request for Apple. – Jon Shier May 10 '15 at 07:38
0

I see 3 ways of achieving what you describe...

Option 1

In your server itself, in its startup code, have it set an alarm() to send itself a SIGTERM at 17:00. This has the advantage it is very clean but has the disadvantage that you need to be in control of the server's source code which may not be the case.

Option 2

Put a bash, or other, wrapper around your server's binary and start the wrapper from launchd. In the wrapper script, start the actual server binary in the background and then wait till 17:00 before sending a SIGTERM to it. This has the advantage of not needing to change the server's source code. The slight disadvantage is that it needs an extra (small) process for the wrapper.

Option 3

Create a second launchd task that starts at 17:00 and sends SIGTERM to the actual server. Not ideal really.

Note It is advisable that your server implement a handler for SIGTERM, whichever way you proceed in order that it shuts down in an orderly fashion - flushing databases, closing files etc.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432