Search code examples
macosbashshellautomator

Running Grunt from OSX Automator shell script


I have a simple Gruntfile that I want to be able to run from an icon in my OSX dock.

I have created a very simple shell script (launcher.sh) that I will save as an application so I can add it to my dock. It works great when I run it directly in my Terminal:

#!/usr/bin/env bash
$(grunt serve --open)

It also works fine with this shebang: #!/bin/bash

However when I call the shell script from an Automator workflow I get the following error:

launcher.sh: line 2: grunt: command not found

This is my Automator set up:

enter image description here

What am I doing wrong?


Update

If I put this in my launcher.sh file:

#!/bin/bash
`/usr/local/bin/grunt serve --open`

I get a different error in Automator: env: node: No such file or directory

But, as before, if I run the script directly in Terminal its fine - so I think @mklement0 is right about the PATH

Update 2

launcher.sh

#!/bin/bash
grunt serve --open

Automator

PATH="/usr/local/bin:$PATH"
~/Public/Sites/launcher.sh

Now I'm still getting an error popup when I run it in Automator, but it has no details - it just says:

The action "Run Shell Script" encountered an error.

The Log panel shows a blank entry. Is there a way to get more info? A verbose mode perhaps?

Update 3

So this is weird... if I use &> ~/log it works. Without it it fails.

But this is working, thanks @mklement0, it'll do for now

PATH="/usr/local/bin:$PATH"
cd ~/Public/Sites && ./launcher.sh &> ~/log

Solution

  • The problem is that the $PATH variable when running from Automator doesn't have the same entries as when running from Terminal.

    Notably, /usr/local/bin is missing, which is where grunt is typically installed (if installed globally).

    A simple workaround is to add the folder in which grunt is installed to your $PATH at the top of the Automator shell script:

    PATH="/usr/local/bin:$PATH"
    ~/Public/Sites/Launcher.sh
    

    Aside from that:

    • Your shell command, $(grunt serve --open), should be just grunt serve --open - no need for a command substitution ($(...) or `...`), as that would actually first execute the command and then try to execute the output from that command.

    • The default working dir. when running a shell script from Automator is ~ (your home folder), which may not be what your script expects; it looks like your script expects its own dir. to be the working dir., so use cd ~/Public/Sites && ./launcher.sh to invoke it.

    • Automator will report an error in case the shell script exits with a nonzero exit code; the error message will include the shell script's stderr output (and nothing else) - sounds like no stderr output is being produced in your case.

      • To capture all output for diagnostic purposes, use something like ./launcher.sh &> ~/log

    On macOS 10.11 through at least 10.15 (as of this update), $PATH has the following value inside a shell script run from an Automator workflow: /usr/bin:/bin:/usr/sbin:/sbin