1

I have been setting up a launch daemon that launch a bash script on demand. However, once I load my launch agent and communicate via socket, then it keep running my bash script every 10 seconds.I've even set KeepAlive flag as false in plist and added delay in bash script, but still it keep running.

How can I set it up to run only once when i connect with the socket.

Plist file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.example.test.helper</string>
        <key>Program</key>
        <string>/Library/Application Support/test/testexec</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Library/Application Support/test/testexec</string>
        </array>
        <key>OnDemand</key>
        <true/>
        <key>KeepAlive</key>
        <false/>
        <key>Sockets</key>
        <dict>
            <key>MasterSocket</key>
            <dict>
                <key>SockFamily</key>
                <string>Unix</string>
                <key>SockPathMode</key>
                <integer>438</integer>
                <key>SockPathName</key>
                <string>/var/run/com.example.test.helper.socket</string>
                <key>SockType</key>
                <string>Stream</string>
            </dict>
        </dict>
    </dict>
</plist>

bash script file:

#!/bin/bash
FILE_PATH="/var/log/test.log"


print()
{
    echo "$(date +"%m-%d %T")    $1" >> $FILE_PATH
}


/bin/rm -r "/private/var/test"
retcode=$?  
print "rm returncode: $retcode"

if [ $retcode -ne 0 ];
then
print "The removing failed"
fi

sleep 15
Dass
  • 326
  • 1
  • 11
  • I suspect this might be to do with the fact that it's a bash script rather than a regular executable. Have you tried specifying `/bin/bash` as the daemon program and passing the script path as the argument? – pmdj Jun 11 '21 at 17:11
  • @pmdj I've tried what you said and it's still relaunching the bash script. – Dass Jun 12 '21 at 05:50

1 Answers1

0

The "on demand" key was used in Mac OS X 10.4 Tiger to control whether a job was kept alive or not. The key should not be used anymore.

With the keepalive key set to false, the default is to start a job only on demand.

Trev
  • 528
  • 4
  • 6
  • I've even removed the "on demand" key and this isn't work. – Dass Jun 14 '21 at 09:49
  • I've just realised that testexec is the bash script in which case it's never going to work. To use sockets on demand the job must check-in to get a copy of the file descriptors using the launch_activate_socket(3) API. – Trev Jun 15 '21 at 07:26