2

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:

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

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

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple$
<plist version="1.0">
   <dict>
   <key>Label</key>
   <string>com.stunnel.stunnelstartup.plist</string>
   <key>Program</key>
   <string>/usr/local/bin/stunnel</string>
   <key>UserName</key>
   <string>root</string>
   <key>RunAtLoad</key>
   <true/>
   <key>AbandonProcessGroup</key>
   <false/>
</dict>
</plist>
2

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 <key>AbandonProcessGroup</key><true/> 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 <key>KeepAlive</key><false/> 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 >/tmp/stunnel.log 2>&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.

  • @Skyler440: D'oh! I got the AbandonProcessGroup key backwards, it should be true not false. I also added a suggestion about capturing debug output from the script and stunnel – Gordon Davisson May 15 '16 at 4:24
1

There are several things which need checking here but this is not a direct answer as not all of them might be the issue here.

For a working example for macports see here

  1. Where is stunnel the path is not the same in launchd as it is in a terminal session
  2. You can't have sudo in the script as run by root - however in this case you don't need the script just run the execrable directly
  3. The username is root not ROOT
  4. Where do you put the plist file?

I would also suggest a daemon should not be run at startup but only when a connection comes in on a socket but that appears to be how stunned is written not an installation problem

Although seeing this other answer I am surprised that the example plist files I have seen work (and my last comment is more relevant)

  • I put the plist file in /library/LaunchDaemons – Skyler 440 May 13 '16 at 16:07
  • The reason I ran a script is so if stunnel tries to start too early, the script compensates for that by adding a delay for the system to boot – Skyler 440 May 13 '16 at 16:08
  • I have no idea where the stunnel path is, or how to find it. All I know is that if you run sudo stunnel from anywhere stunnel will start – Skyler 440 May 13 '16 at 16:08
  • and the issue is that the path in terminal and launchd differs so one can find it and the other can't so you need to know this. try type -p stunnel to find it – user151019 May 13 '16 at 16:16
  • Thanks, I found the path. I have tried all the suggestions with no luck. See edit – Skyler 440 May 14 '16 at 5:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .