launchd error

I can't figure out why this launchd fails.

The process itself isn't important, I'm just curious why it fails on macOS Monterey (used to run on High Sierra)


The launchd moves file from the Downloads Folder to the Desktop:


<?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>EnvironmentVariables</key>
	<dict>
		<key>PATH</key>
		<string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin</string>
	</dict>
	<key>Label</key>
	<string>com.tonyt.movetodesktop</string>
	<key>ProgramArguments</key>
	<array>
		<string>/bin/mv</string>
		<string>/Users/Tony/Downloads/*</string>
		<string>/Users/Tony/Deskop</string>
	</array>
	<key>RunAtLoad</key>
	<false/>
	<key>StartCalendarInterval</key>
	<array>
		<dict>
			<key>Hour</key>
			<integer>18</integer>
			<key>Minute</key>
			<integer>0</integer>
		</dict>
	</array>
</dict>
</plist>



I used Lingon to create the plist, and testing in Lingon gives the following error (there are files in Downloads):


Posted on Nov 11, 2021 7:24 AM

Reply
Question marked as Top-ranking reply

Posted on Nov 11, 2021 8:50 AM

Tony T1 wrote:

I can't figure out why this launchd fails.
The process itself isn't important

Actually it is.

I'm just curious why it fails on macOS Monterey (used to run on High Sierra)

It used to run?


There are many reasons why this could fail. One key reason is the new security infrastructure. Desktop and Downloads are protected folders. A launchd executable will need Full Disk Access to read or write them.


Another reason is launchd itself. Is this a launch daemon or a launch agent? That wasn't as big a deal in High Sierra. Either probably would have worked. It is a huge deal now. This must be a launch agent.


Next is the program you are running. You are running /bin/mv, which doesn't understand wildcards. You would have to run an actual shell that could global those wildcards. Launchd is a factor here too. It may, under some circumstances run a shell for you. And that may have changed since High Sierra.


Another possibility is the shell itself. I think it was still bash in High Sierra and it is now zsh. All shells have different behaviour when run interactively vs. non interactively, as in launchd. File globbing seems particularly tricky. Even trying to track down actual command line arguments and ways to set options on a shell, from the command line, is virtually impossible. This is crazy.


If you are going to run something from launchd, it should be a standalone executable. That allows an easy solution to a long, long list of launchd problems. If you must use a shell script, use Perl or maybe Python. Why people try to write programs in sh, bash, or zsh is beyond me. Running these tools from launchd is particularly tricky.

Similar questions

4 replies
Question marked as Top-ranking reply

Nov 11, 2021 8:50 AM in response to Tony T1

Tony T1 wrote:

I can't figure out why this launchd fails.
The process itself isn't important

Actually it is.

I'm just curious why it fails on macOS Monterey (used to run on High Sierra)

It used to run?


There are many reasons why this could fail. One key reason is the new security infrastructure. Desktop and Downloads are protected folders. A launchd executable will need Full Disk Access to read or write them.


Another reason is launchd itself. Is this a launch daemon or a launch agent? That wasn't as big a deal in High Sierra. Either probably would have worked. It is a huge deal now. This must be a launch agent.


Next is the program you are running. You are running /bin/mv, which doesn't understand wildcards. You would have to run an actual shell that could global those wildcards. Launchd is a factor here too. It may, under some circumstances run a shell for you. And that may have changed since High Sierra.


Another possibility is the shell itself. I think it was still bash in High Sierra and it is now zsh. All shells have different behaviour when run interactively vs. non interactively, as in launchd. File globbing seems particularly tricky. Even trying to track down actual command line arguments and ways to set options on a shell, from the command line, is virtually impossible. This is crazy.


If you are going to run something from launchd, it should be a standalone executable. That allows an easy solution to a long, long list of launchd problems. If you must use a shell script, use Perl or maybe Python. Why people try to write programs in sh, bash, or zsh is beyond me. Running these tools from launchd is particularly tricky.

Nov 11, 2021 9:58 AM in response to etresoft

Just tried specifying a single file, and error (in Lingon) was "mv: rename /Users/Tony/Downloads/test.txt to /Users/Tony/Desktop/test.txt: Operation not permitted"

And as you suggested, giving mv full disk access gets is to work on a single file

Giving bash full disk access gets a sh script to move all files

and FWIW, if I wrap a bash script in a Shortcuts Run Shell Script, it works (as Shortcuts already has full disk access)


Thanks for helping me understand this

Nov 11, 2021 9:36 AM in response to etresoft

By the process, I meant moving files with launchd wasn’t important to me, I wasn’t looking for alternatives (I was able to do so with launchd running a Shortcuts Action).


Your comment on Downloads and Desktop now being protected is most likely the reason. (I tried with mv in a bash script that works in Terminal (has full disk access) but failed in lauchd) Saved as a LaunchAgent, and what worked in High Sierra was the mv in a bash script (forgot about mv not understanding wildcards, which explains the 'no file or directory error' I posted).

Nov 11, 2021 9:32 AM in response to Tony T1

Tony T1 wrote:

Your comment on Downloads and Desktop now being protected is most likely the reason. (I tried with mv in a bash script that works in Terminal (has full disk access) but failed in lauchd)

I don't know. I tried your script and got it to work very easily, but only when specifying the file. Enabling Full Disk Access was all that needed.


It was the wildcard in the path that made me give up. My solution that would just be a Perl script that could accept any string as input and do the globbing itself. Trying to find out anything about how zsh actually works, with all the options, in a non-interactive context, is just about impossible.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

launchd error

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.