KM Macro Execution Issue – Stuck in "Execute Shell Script"

Hi All,

I am having some troubles with a couple of the macros I built to update my DNS settings to use DNSCrypt , a local DNS service that encrypts DNS requests to upstream DNS providers.

The macros and their functions are:

  1. "DNSCrypt enable & set interface DNS"
    As the name suggests, the macro does 2 things:
    a) Run DNSCrypt process, echo a message.
    b) Set the DNS server to local dnscrypt for the selected interface, and echo a message.

  2. "Reset Interface DNS"
    This macro does 2 things:
    a) Reset the DNS server to DHCP setting for the selected interface, echo a message.
    b) Kill the DNSCrypt process, echo a message.

See screenshots below for the 2 macros.

image

image

My problem with the macros is that when executing "DNSCrypt enable & set interface DNS", task 1 is partially executed and the rest of the macro is stuck, i.e. DNSCrypt process is started, but the echo message is not executed, task 2 never runs.

The rest of the tasks for macro 1 will run if I either re-run the macro, or run the 2nd macro "Reset Interface DNS" (i.e. the 2 tasks in "Reset Interface DNS" run; and then, the echo message in Macro from task 1 in "DNSCrypt enable & set interface DNS" will pop up, and task 2 will start to execute... the echo message pop-ups will end up like the following screenshot after running "Reset Interface DNS" macro...)

I have tried various things:

  • Adding Pause between tasks
  • Adding "sleep" for 1 sec in the Shell Script
  • Swapping the order of the 2 tasks in macro "DNSCrypt enable & set interface DNS"
  • Different ways to run the DNSCrypt process (direct CLI command to start the process, or call the process from an external bash script - see screenshot)
    image

None of them work.

Any ideas on what the issue can be, or ways to diagnose further will be much appreciated.

Thanks.

You're calling a shell script. Things will pause at that point until the shell script terminates -- if it doesn't you'll not get your echo and the macro's execution will be blocked.

Which again suggests that your shell script running DNScrypt as a foreground process -- once you terminate it with the second macro your script is able to continue.

Difficult to say more without seeing the shell script (and never having used DNScrypt!) but have a look to see if you can background the process when you run it, so your script can continue without having to wait.

That's a good point, @Nige_S ! (Sorry, 101 stuff I should have thought about)

I tried running the 1st macro, stuck at the same step; manually killing the process has processed the macro execution. This seems to indicate the process running in the foreground.

However, I tried different ways to run process in the background, which worked in a terminal environment, but in KM the macro is still stuck at the process execution... Screenshot below is an example adding the ampersand (&) in the end. Note - "dnscrypt-proxy" is the executable to run DNSCrypt.
image

I've tried the following methods to run the process in the background.

nohup sudo /Users/xxx/dnscrypt/dnscrypt-proxy
nohup sudo /Users/xxx/dnscrypt/dnscrypt-proxy &
sudo /Users/xxx/dnscrypt/dnscrypt-proxy &
sudo /Users/xxx/dnscrypt/dnscrypt-proxy & disown
tmux new -d 'sudo /Users/xxx/dnscrypt/dnscrypt-proxy'

JFYI - the shell script file is a one liner to start the executable.
image

Worth mentioning, I am using iTerm2 as the default terminal app. Unsure if it is related. Couldn't find an effective way to revert the default to Terminal app, and cannot uninstall iTerm2 due to other dependencies for my customisation.

I did try updating macos system preference to open all ".sh" files using Terminal.app; and KM "Execute Shell Script" -> "Execute script file". Unfortunately problem still there.
image

I have added another workaround - using Apple Script calling Terminal app to start the DNSCrypt executable. This works; and I'll have to add an additional action to kill the Terminal app due to frequent lockup (a separate issue).

Which will let the echo statement run. But the dnscrypt process is still running in that instance of the shell, albeit in the background, and the "Execute Shell Script" action won't finish until that terminates -- hence the block.

Remember -- each "Execute Shell Script" action is a new shell. Multiple actions in a macro are not the same a typing multiple lines into an iTerm window -- more like a new iTerm window per action.

So -- ways round this.

I like to know if a process I've started is still running, so I'd probably use an "Execute an AppleScript" action to pop a new Terminal window and run the command in that. Because you are hiving it off to an external program the action will complete and the macro will move on. (Edit -- I see you've already found that method yourself.)

If you want to keep things more hidden, move your "Execute Shell Script" action out to a separate macro which you can then call asynchronously with an "Execute a Macro" action. You might (untried) have to cancel that macro as part of your "stopping" routine -- I don't know if the killall will let then let the sub-macro run to completion.

To KM's "Execute Shell Script" action? Not at all -- which is both good (a clean slate) and bad ($PATH not the same as your "normal" environment etc). This action is not the same as double-clicking a script file in the Finder -- if you want to force the script to run in iTerm2 you could use the "Open File..." action.

Do note that iTerm2 is still AppleScriptable (although that's deprecated in favour of Python). So you should be able to use it while you sort out your Terminal issues.

Thanks @Nige_S. :+1:

This describes it well!

Yes - the option of AppleScript makes more sense considering how the processes are managed. Thanks for the idea of sub-macro, too. Cheers:)

1 Like

Yes, the killall will let the "background" macro progress.