Launch is a wrapper for the launch(3) API for daemons and agents spawned by launchd(8).
Launch agents and daemons MUST NOT:
Call daemon(3)
Do the equivalent of daemon(3) by calling fork and having the parent exit
Launch agents and daemons SHOULD NOT do the following as part of their starup initialization:
Set the user ID or group ID (Process::Sys.setuid, Process::Sys.setgid and friends)
Setup the working directory (Dir.chdir)
chroot(2)
setsid(2) (Process.setsid)
Close “stray” file descriptors
Change stdio(3) to /dev/null (STDOUT.reopen and friends)
Setup resource limits with setrusage (Process.setrlimit)
Ignore the SIGTERM signal (trap ‘TERM’, ‘IGNORE’)
The above is from launchd.plist(5). Please read it for further details.
To shut down cleanly trap 'TERM' and perform any shutdown steps before exiting.