[launchd-dev] A few pointers at a launchd script

Panagiotis Atmatzidis atma at convalesco.org
Fri Oct 30 15:31:23 PDT 2009


I'm forwarding a message that I posted a few minutes ago at users- 
macports ML because I just found this mailing list and I think that  
it's more apropriate... (and a good one to keep an eye to avoid  
launchd adventures..)

--
Greetings,

I've created my first launchd script. It's for OpenVPN2. Here are the  
files I've created so far:

devo:/opt/local/etc/LaunchDaemons root# ls -l org.macports.OpenVPN2/
total 16
-rwxr-xr-x   1 root  admin   957 Oct 30 23:06 OpenVPN2.wrapper
-rw-r--r--   1 root  admin  1026 Oct 30 23:39  
org.macports.OpenVPN2.plist

The Wrapper
-----
#!/bin/sh

. /etc/rc.common

load() {
                if [ -d /System/Library/Extensions/tun.kext ]; then
                        kextload -q /System/Library/Extensions/tun.kext;
                else
                        echo "tun.kext not found in /System/Library/ 
Extensions/"
                fi
        }

StartService() {
                load; # first load the module
                if [[ $( kextstat -l | grep -q 'tun' )$? == 0 ]]; then
                /opt/local/sbin/openvpn2 --config /opt/local/etc/ovpn/ 
server.conf --writepid /opt/local/etc/ovpn/ovpn.pid --daemon OpenVPN2
                                /usr/bin/logger "OpenVPN is loaded"
        else
                                /usr/bin/logger "tun extensions is not  
loaded."
                fi
}

StopService() {
  if [[ $( kextstat -l | grep -q 'tun' )$? == 0 ]]; then
        kextunload /System/Library/Extensions/tun.kext # first unload  
the module
  fi

  pid=`cat /opt/local/etc/ovpn/ovpn.pid` # get the pid number
  if [ $? -eq 0 ]; then
    kill $pid
  fi
}

RestartService() {
  StopService "$@"
  StartService "$@"
}

RunService "$1"


the .plist which is an: ln -sf /opt/local/etc/LaunchDaemons/ 
org.macports.OpenVPN2/org.macports.OpenVPN2.plist /Library/ 
LaunchDaemons/....
------------------------
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
<plist version='1.0'>
<dict>
<key>Label</key><string>org.macports.OpenVPN2</string>
<key>ProgramArguments</key>
<array>
        <string>/opt/local/bin/daemondo</string>
        <string>--label=OpenVPN2</string>
        <string>--start-cmd</string>
        <string>/opt/local/etc/LaunchDaemons/org.macports.OpenVPN2/ 
OpenVPN2.wrapper</string>
        <string>start</string>
        <string>;</string>
        <string>--stop-cmd</string>
        <string>/opt/local/etc/LaunchDaemons/org.macports.OpenVPN2/ 
OpenVPN2.wrapper</string>
        <string>stop</string>
        <string>;</string>
        <string>--restart-cmd</string>
        <string>/opt/local/etc/LaunchDaemons/org.macports.OpenVPN2/ 
OpenVPN2.wrapper</string>
        <string>restart</string>
        <string>;</string>
        <string>--pid=none</string>
</array>
<key>Debug</key><false/>
<key>Disabled</key><false/>
<key>OnDemand</key><false/>
<key>RunAtLoad</key><true/>
<key>NetworkState</key><true/>
</dict>
</plist>




However, there are two issues that I can't seem to be able to manage  
right now.

The first is that OpenVPN does not start at boot while the module is  
loaded succesfully. When I login to the system and kill daemondo, it  
relaunches itself and ovpn works fine. I suspect that the problem is  
en0. Launchd tries to launchd openvpn before en0 comes up. That's why  
I put the NetworkState keyword, but it does not seem to effect *any*  
startup script. I had issues with dnsmasq also in the recent past.

The second problem with this script is that when I unload it via  
launchd it does not kill the process. Launchd unloads the script and  
(probably) will not be launchd (if -w is added) in the next boot but  
daemondo keeps running the process nevertheless. Is this a normal  
behaviour?

I'm not *that* worried about the second. I'd prefer to have a solution  
about the first one, which is the most important for me.

Best regards & thanks in advance
Panagiotis (atmosx) Atmatzidis

email:	atma at convalesco.org
URL:	http://www.convalesco.org
GnuPG key id: 0xFC4E8BB4
--
The wise man said: "Never argue with an idiot. They bring you down to  
their level and beat you with experience."



More information about the launchd-dev mailing list