1

I'm trying to watch a certain folder and run a script if there is a change to the folder. For this, I'm using below fswatch command (OS is Monterey [12.0.1]):

fswatch -o ~/Desktop/iosBuildZips | xargs -n1 -I{} ~/Desktop/scripts/script.sh

as per the answer on this question.

fswatch successfully registers a file change in the defined folder, but there is an error when fswatch tries to call script.sh:

xargs: /Users/myusername/Desktop/scripts/script.sh: Operation not permitted

Screenshot

enter image description here


I'm not that familiar with macOS, so I tried using ''chmod 777'' on all involved folders.

But this produces the same error.

Screenshot

enter image description here


I also get the error when I precede the fswatch command with sudo...

Why am I getting this error, and how can I fix it?

I don't think it's relevant what is in script.sh, since I get the same error even if script.sh contains only one echo abc command, but for completeness, here is the contents of script.sh.

echo Unzipping received build file
unzip ~/Desktop/iosBuildZips/iosBuild.zip -d ~/Desktop/iosBuild
cd ../iosBuild
pod install
2
  • 2
  • Do you have execute permission to the script itself (not the directory it's in)? Does adding a shebang line (e.g. #!/bin/bash) to the beginning of the script help? What about putting it in the top of your home directory instead of the Desktop subdirectory (note: Desktop, Documents, Downloads, and a few other locations are considered private, and have special access restrictions which can cause trouble under complicated circumstances)? Jul 9, 2022 at 1:18

3 Answers 3

2

The reason why xargs returns Operation not permitted is that script.sh doesn't have the execute bit set.

How do I set the execute bit?

To make the script executable, simply run:

chmod a+x ~/Desktop/scripts/script.sh

which sets the execute bit (x) for all (a) users.

What is the execute bit anyway?

UNIX-like operating systems traditionally use a "mode" system to, among other things, describe whether a file is readable, writable or executable. In this context, it's usual to talk about the "execute bit" to describe whether a file is executable.

To see the mode of a file, simply run ls -l: the mode is coded in the first block of information displayed. For example, if you run ls -l /bin/cp, you will see that the output looks similar to this:

-rwxr-xr-x  1 root  wheel   152672 May  9 23:30 /bin/cp

In the example above, -rwxr-xr-x is the file mode, which in this particular case makes the file readable and executable for all, and writable for the owner, that is, the root user.

For more information on file modes and how to interpret them, see https://en.wikipedia.org/wiki/File-system_permissions and https://en.wikipedia.org/wiki/Chmod.

0
1

~/Desktop is a protected folder.

xargs can't read the script, that's what "Operation not permitted" means.

$ # terminal does not have full disk access...
$
$ echo abc | xargs -n1 -I{} ~/Desktop/scripts/test.sh
ksh: /Users/mwilson/Desktop/scripts/test.sh: cannot open [Operation not permitted]
$
$ # give terminal full disk access...
$
$ echo abc | xargs -n1 -I{} ~/Desktop/scripts/test.sh
test!
$

Give xargs the power to read the script or don't store the script in a protected folder.

2
  • 1
    "Give xargs the power to read the script", how?
    – nohillside
    Jul 8, 2022 at 18:24
  • Thanks Marc Wilson, but I had already tried that to no avail. However, I found a solution and am making an answer.
    – ChrisC
    Jul 8, 2022 at 18:25
1

I'm not sure why (since I am not too familiar with zshell, bash, and so on), but it seems I had to call script.sh from bash. i.e.:

fswatch -o ~/Desktop/iosBuildZips | xargs -n1 -I{} bash ~/Desktop/scripts/script.sh

Now the commands in script.sh actually execute when there is a change in ~/Desktop/iosBuildZips.

4
  • If someone with an understanding of why it works this way wants to expand on this answer, please do.
    – ChrisC
    Jul 8, 2022 at 18:29
  • 3
    Could it be that ~/Desktop/scripts/script.sh doesn't have the executable bit set (that would explain that you need to prepend bash to it)? If that's the case, you can set it with chmod a+x ~/Desktop/scripts/script.sh.
    – jaume
    Jul 8, 2022 at 18:43
  • 1
    @jaume yep, that was it, thanks! Feel free to add this as an answer if you want and I'll mark it as accepted!
    – ChrisC
    Jul 10, 2022 at 8:04
  • I'm glad it helped. I've added an answer with provided some background information about the execute bit and file modes.
    – jaume
    Jul 10, 2022 at 15:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .