"Launch Task /bin/sh -c" error in Engine.log

Today I saw this error in Engine.log:

2024-07-02 06:03:47 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-3F2A8E75-C1DC-422D-BB6E-40AB40B98657' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

When I asked Claude.ai what this means, I got this (if this helps somehow, if it's even accurate?):

This error message from Keyboard Maestro's Engine.log file indicates an issue with launching a shell script task. Let's break it down:

  1. The task was attempting to launch a shell script located at:
    /var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-3F2A8E75-C1DC-422D-BB6E-40AB40B98657

  2. The launch failed with an exception.

  3. The specific error is: "Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9"

This error suggests a problem with file descriptors when trying to spawn a new process. Error number 9 (errno 9) typically corresponds to a "Bad file descriptor" error.

The issue might be related to:

  1. Permissions problems
  2. An issue with how Keyboard Maestro is trying to set up file actions for the new process
  3. A problem with the temporary script file itself

To troubleshoot this, you could try:

  1. Checking permissions on the temporary folder and script file
  2. Restarting Keyboard Maestro
  3. Recreating the macro that's causing this error
  4. Checking if this occurs with other scripts or only this specific one

Does this help somehow?
If not, what can I do to fix this?

I would suggest restarting Keyboard Maestro and Keyboard Maestro Engine, or restarting your Mac.

It sounds like the system is having an issue with file descriptors, which is a low level system way of referencing open files.

If the failure is happening with a trivial script (eg:

echo Hello

), then the issue isn't with the script, and clearly Keyboard Maestro normally can execute scripts, so the issue is some sort of system thing - the system can run out of file descriptors for example (which normally only happens if you use a lot of them quickly, as it can take some time to recover them after use).

This is all too complex for me to understand, but here's some added information:

In the console all the entries that have that issues are these:

2024-06-03 15:19:42 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-F5E0A058-AD39-4AEB-BC60-9880A5DCE817' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-06-05 14:01:54 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-C183FA35-EE15-4CF6-913B-3D47A33AA5CC' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-06-08 13:06:12 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-5B39C22C-3FF5-4C46-8443-93A04788D640' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-06-12 16:40:22 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-F36596D6-9832-423C-922E-8065B9BC5B17' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-06-14 19:24:33 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-EAFB5E01-261B-4CB0-BEEA-256A71C02FC2' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-06-14 19:30:00 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-A9D7EB46-1FC9-47C3-BF8E-F560C2F3A936' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-06-16 15:10:00 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-35FA3611-B82B-43BC-B859-223803AC1632' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-06-20 19:46:40 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-01528E5B-8B3A-4604-B5F7-96396F0729D4' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-06-20 20:00:00 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-D47BE801-D050-4504-B4E2-D10CC61B2E76' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-06-20 20:00:00 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-B8FE171E-A08E-484F-8A87-124C8C2F9E6C' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-06-22 18:10:54 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-46730A92-8FCC-4CDA-A6C5-C235C273F523' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-06-30 23:56:12 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-B77CB665-D90E-4C3C-A2D0-D22091D522C4' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9

2024-07-02 06:03:47 Launch Task /bin/sh -c '/var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/Keyboard-Maestro-Script-3F2A8E75-C1DC-422D-BB6E-40AB40B98657' failed with exception Failed to set posix_spawn_file_actions for fd -1 at index 0 with errno 9


I noticed that the numbers at the end always seem to be different, so it's not always affecting the same file in that /var/folders/4_/68zgw8m93p70d84rycx_fsh80000gn/T/ path. I don't know if this is relevant or not, but that's one thing I noticed.

All of these error messages seem to be related to the action "15433233", just because after that Launch Task /bin/sh -c message, I always get this:
2024-06-20 20:00:00 Action 15433233 failed: Task failed with status -1

The action is this:
image
which I mention here: Issue with shell script action - Task failed with status -1

This particular macro that seems to have this issue, starts with quitting the Engine, then I have an external script that is set up to restart the engine. I have already restarted both the Editor and the Mac, but I keep getting this issue quite often (I would say 50% of the time when I run that macro).

So is there any advice on how to maybe set that script that could potentially fix the issue?

Are you, perhaps, "For Each"-ing through a list of paths and executing the shell script for each item? I can see a case where the "For Each" loop is short and you are spawning new shells faster than the OS can dereference the old "finished" ones.

If that's the case you could test by slowing down the loop, adding in a 1 second pause each time around. But a better solution would be to refactor your macro and pass the list to shell script and have the script do the loopy directory making.

1 Like

Yes this is the main action

image

I'm afraid that adding 1 second could be an issue for this particular case, because since I'm copying modified files within a time range (and that can be a few minutes to maybe a few hours or even days), it could mean that I have hundreds or even thousands of files to copy, because even those files that the system is constantly updating would be included. I would end up with a macro that would be running forever if I added 1 second per file being copied.

So you mean that the "FOR EACH" would be replaced by a single Shell Script that does that same thing (for each...), but reading from an external .sh file, including the mkdir -p "path/to/folder" command?

Is there a particular reason to use a shell script to create the folder? You could try using the KM action to create the folder to see if that avoids any of the problems you're seeing:

image

On the gear menu for that action, select "Create Intermediate" to match the "-p" option of the mkdir command.

1 Like

So make it half a second, or 2/100ths, or only pause every 10 loops... This is for testing the theory, not a solution!

No -- you save the list to a temporary file, your shell script opens that files and loops through the contents line by line. Here's a demo, with pauses so you can see the temp file appear. Also note how the first folder has a space in its name but we haven't had to do anything "special" to cope with that:

Make Dirs from list.kmmacros (4.1 KB)

Image

Not really. I think I just followed the advice of some users here on the forum, or somewhere else online and since this is a new territory for me, I just used whatever was suggested :wink:
I will make the change you suggested and will try it.
Thanks for the tip! :raised_hands:

1 Like

Or I could add an action to pause until the path exists?

While any pause will help, the existence or not of the path has nothing to do with this.

Just a quick update: since I changed to the Create Folder action, it seems that the issues are gone. I ran the macro 5 or 6 times and by now I would have seen the error message, but not yet. Hopefully this was the fix it needed. Thanks! :muscle:

1 Like