Process().Run fails to *actually* execute until parent process terminates

I'm getting some weird behavior when trying to execute another command-line tool from my exec app. It works fine, but for some reason when using the shortcuts run YourShortcut, we only get to the specified line, where the command “hangs”, until the parent process exits.

Now the really strange part: When using other CLI commands it executes as we’d expect. Using other Shortcuts commands shortcuts list works, & we get the data. It’s only when we try to shortcuts run YourShortcut that the CLI command fails.

I’ve been stumped on this for a good month to two. My workaround was to use AppleScript, but it’s really not efficient. It’s very verbose & way over-complicated to what should just work. Ideally, I want to gut out the AppleScript core & replace it with the Shortcuts CLI.

To give a brief overview of the app's architecture, which is a plugin for another 3rd party app:

  • StreamDeck.app
    • Our custom .exec
      • Spawned CLI process
    • (optional) Sister process, which communicate via it's own CLI tool or websockets.

I’ll also add, that spawning a sister process/app that I was passing the shortcutName to, had the same effect. For the life of me, I can’t seem to solve this. The only other thing that seems like it would work is running a web-socket connection to the side app. I’ve done some preliminary testing & that appears to work, but again, this seems overkill for such a simple problem...

    func launchCommand (inputShortcut: String) {
        let shortcutCLI = Process()
        shortcutCLI.executableURL = URL(fileURLWithPath: "/usr/bin/shortcuts")
        shortcutCLI.arguments = ["run", command]
        
        do{
            try shortcutCLI.launch()
            //None of the code below runs after the fact either, which makes sense.
            //Capture Pipe output & print logic...
        } catch {
            //It refuses to throw an error, because it's waiting to execute, when the app closes?
            NSLog("Failed to run with Error \(error)")
        }
    }

I've tried variations of the command, without fail, it won't actually launch the Shortcut until the parent process exits/terminates

I got some pointers & tried some things based off stackotter's feedback, which you can see below, but we were unable to resolve it.

    func launchCommand (inputShortcut: String) {
        let shortcutCLI = Process()
        shortcutCLI.executableURL = URL(fileURLWithPath: "/usr/bin/nohup")
        shortcutCLI.arguments = ["/bin/bash", "-c", "/usr/bin/shortcuts run \(inputShortcut) >/dev/null 2>&1 &"]
        //The rest of the function
    }

If you want to test/run it, I've attached the repo. Please note: You will need a StreamDeck device, either physical or the mobile app, which has a free 1-month trial. You'll also need to be running macOS 12.0+.

I'd appreciate any feedback/suggestions on how to best proceed.

Thanks,
SENTINELITE

What I usually do in this situation is run sample against the hung process to find out where it’s hung. That might give you a clue as to why it’s hung.


Having said that, I tried this here in my office and didn’t reproduce the problem you’re having:

  1. On macOS 12.1, I ran Shortcuts and created a QTest shortcut that runs the “Play sound” action.

  2. Using Xcode 13.2.1, I created a tiny test project with a button wired up to this code:

    let p = Process()
    p.executableURL = URL(fileURLWithPath: "/usr/bin/shortcuts")
    p.arguments = [ "run", "QTest" ]
    p.terminationHandler = { _ in
        print("did end, status: \(p.terminationStatus)")
    }
    print("will start")
    try! p.run()
    print("did start")
    
  3. When I run the app and click the button, it plays the sound and I see this in Xcode’s console:

    will start
    did start
    did end, status: 0
    

Clearly there’s something specific to your app in play here, but it’s hard to say what. If you get truly stuck, I encourage you to open a DTS tech support incident so that I can allocate the time to dig into the specifics of your situation.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Hey, Eskimo/Quinn!

Firstly, I apologize for the delayed response.

Upon digging deeper, the Shortcuts CLI method doesn’t (at least to my knowledge & the man page) run Shortcuts with the UUID. As I’m going to be linking my saved data with the UUID via a cross-reference, I’ve chosen to go a different route, via the Shortcuts deep-linking Scheme

I really wish I knew why the above was failing to execute, but I guess we’ll never know.

Should I file a Radar, asking for support for the Shortcuts CLI to fetch & run via UUIDs?

Thanks,
SENTINELITE

Should I file a Radar, asking for support for the Shortcuts CLI to
fetch & run via UUIDs?

Yes. In general, if the system doesn’t behave the way you want it to behave, you should file a bug report / enhancement request describing the issue.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

I'll be reaching our for a DTS request. The URL scheme won't work, as it brings the Shortcuts App the the foreground, which isn't the correct behavior. Currently in the process of remodeling our house.

The Feedback number: FB9865328

Thanks,
SENTINELITE