Start stunnel service on bootup I have a service ran under homebrew. This service is simply started by running the command

sudo stunnel

I need this command to run on bootup, so I created a LaunchDaemon but it is not working. Here is the launchd file:

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple$
&lt;plist version="1.0"&gt;
   &lt;dict&gt;
   &lt;key&gt;Label&lt;/key&gt;
   &lt;string&gt;com.stunnel.stunnelstartup.plist&lt;/string&gt;
   &lt;key&gt;ProgramArguments&lt;/key&gt;
   &lt;array&gt;
          &lt;string&gt;/bin/sh&lt;/string&gt;
          &lt;string&gt;/usr/local/etc/stunnel/startup.sh&lt;/string&gt;
   &lt;/array&gt;
   &lt;key&gt;UserName&lt;/key&gt;
   &lt;string&gt;ROOT&lt;/string&gt;
   &lt;key&gt;RunAtLoad&lt;/key&gt;
   &lt;true/&gt;
   &lt;key&gt;KeepAlive&lt;/key&gt;
   &lt;false/&gt;
&lt;/dict&gt;
&lt;/plist&gt;


And here is the shell script:

#!/bin/bash
#stunnel startup
sleep 30 #Delay added for the system to boot properly
sudo stunnel


What am I doing wrong???

EDIT: I have done what all of the answers have suggested so far, with still no luck. Here is my new launchd file:

I also edited the stunnel configuration so it does not run in the background

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple$
&lt;plist version="1.0"&gt;
   &lt;dict&gt;
   &lt;key&gt;Label&lt;/key&gt;
   &lt;string&gt;com.stunnel.stunnelstartup.plist&lt;/string&gt;
   &lt;key&gt;Program&lt;/key&gt;
   &lt;string&gt;/usr/local/bin/stunnel&lt;/string&gt;
   &lt;key&gt;UserName&lt;/key&gt;
   &lt;string&gt;root&lt;/string&gt;
   &lt;key&gt;RunAtLoad&lt;/key&gt;
   &lt;true/&gt;
   &lt;key&gt;AbandonProcessGroup&lt;/key&gt;
   &lt;false/&gt;
&lt;/dict&gt;
&lt;/plist&gt;
It appears that stunnel backgrounds itself, i.e. it forks a subprocess to do the actual work in the background, then exits. When a launchd-managed process exits, launchd defaults to automatically killing off any leftover subprocesses... like the background stunnel process. I haven't tested these, but there are two simple ways to avoid this.

First, you can tell launchd not to kill leftover subprocesses by adding &lt;key&gt;AbandonProcessGroup&lt;/key&gt;&lt;true/&gt; to the .plist.

Second, you can tell stunnel to not background itself by adding foreground = yes to its config file. If you do the second option, you can probably also remove &lt;key&gt;KeepAlive&lt;/key&gt;&lt;false/&gt; from your .plist, and let launchd manage the stunnel daemon properly.

[Update] If that and Mark's suggestions don't get it working, I'd recommend adding some debugging output in the script, and sending that and stunnel's output to a log file. Something like this:

#!/bin/bash
#stunnel startup
exec &gt;/tmp/stunnel.log 2&gt;&amp;1 # Send all output to a debug log
sleep 30 #Delay added for the system to boot properly
echo "$(date) - Starting stunnel"
sudo /path/to/stunnel
echo "$(date) - stunnel exited with status $?"
echo


You can do similar things by adding StandardOutPath and StandardErrorPath keys, but in this case it's easier to edit the script.