MacOS – How to delete directory content with launchd

launchdmacosscript

I am trying to empty a directory at the end of the day, so I have written the following script:

<?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.my.script.removetemp</string>
    <key>ProgramArguments</key>
    <array>
        <string>rm</string>
        <string>-fr</string>
        <string>/tmp/test/*</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Minute</key>
        <integer>45</integer>
        <key>Hour</key>
        <integer>23</integer>
    </dict>
</dict>
</plist>

I named it com.my.script.removetemp.plist and placed it in ~/Library/LaunchAgents/, but it doesn’t work.

What am I doing wrong?

Best Answer

Shell globbing (<string>/tmp/test/?*?</string>) is not supported by launchd!

You can either remove the whole folder test <string>/tmp/test</string> or launch a shell script rmtestcontent (in the example /usr/local/bin/rmtestcontent) with the content:

#!/bin/bash

/bin/rm -fr /tmp/test/*

with the plist:

<?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.my.script.removetemp</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/sh</string>
        <string>-c</string>
        <string>/usr/local/bin/rmtestcontent</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Minute</key>
        <integer>45</integer>
        <key>Hour</key>
        <integer>23</integer>
    </dict>
</dict>
</plist>

to empty the directory /tmp/test.