[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..)


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  

The Wrapper

. /etc/rc.common

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

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"
                                /usr/bin/logger "tun extensions is not  

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

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

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

RunService "$1"

the .plist which is an: ln -sf /opt/local/etc/LaunchDaemons/ 
org.macports.OpenVPN2/org.macports.OpenVPN2.plist /Library/ 
<?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'>

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  

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