LaunchAgents zip/tar not working in plist

backupbashcommand linelaunchdterminal

I created the plist for a 'custom' backup agent (via LaunchAgent). The (zip) program is called, but I keep getting a 'zip error: Nothing to do!' in my logs.

I assume it has something todo with permissions. I did add sh and zip to my Full Disk Access security settings. But it did not help.

On a side-note, when I move the zip command in a shell (sh) script, it works.

I would like to know why using the zip command directly doesn't?

Running the command for the terminal works also without issues, like so:

/usr/bin/zip /Users/username/Backup/test.zip /Users/username/Documents/*.pdf

LaunchAgent 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.myuser.DailyBackup</string>
    <key>Program</key>
    <string>/usr/bin/zip</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/username/Backup/test.zip</string>
        <string>/Users/username/Documents/*.pdf</string> 
    </array>    
    <key>StandardOutPath</key>
    <string>/tmp/my.backup.log</string>    
    <key>StandardErrorPath</key>
    <string>/tmp/my.backup.err</string>    
    <key>Debug</key>
    <true/>    
    <key>StartInterval</key>
    <integer>10</integer>
</dict>
</plist>

Best Answer

From man launchd.plist

ProgramArguments <array of strings>
This key maps to the second argument of execvp(3) and specifies the argument vector to be passed
to the job when a process is spawned. This key is required in the absence of the Program key.
IMPORTANT: Many people are confused by this key. Please read execvp(3) very carefully!

So you need to use

    <key>Program</key>
    <string>/usr/bin/zip</string>
    <key>ProgramArguments</key>
    <array>
        <string>zip</string>
        <string>/Users/username/Backup/test.zip</string>
        <string>/Users/username/Documents/*.pdf</string> 
    </array>    

It still won't work though because launchd doesn't expand wildcards and zip doesn't either. It's easier to use launchd to start a shell script which does all the work then.