36

I have an Automator workflow and one of the actions is to clone a repository. Here's the code for the 'Run Shell Script' action. The command is run within the Bash shell /bin/bash if that matters:

SOURCEDIR="${3/\/Volumes/}"

cd "$2"
REPOURL="[email protected]:$SOURCEDIR"
WORKINGDIR="$1"

# Capture any errors with cloning process in log file
git clone "$REPOURL" "$WORKINGDIR" &> ./log.txt

// For debugging
echo "exit code: $?"
echo "PPID: $PPID"

open .

Now the Automator App runs fine on my machine. The path to git on my machine (A Macbook Pro) is: /usr/bin/git (I believe I installed git on my machine through Xcode)

But on my co-worker's machine (Also a Mac Pro) the Automator App fails. In fact, the output of the log.txt file says: bash: git: command not found

Now on my co-worker's machine the path to git is: usr/local/git/bin, which, yes, is different because he installed git through the Google Git Installer for Mac OS X, but I didn't think it should matter because in the script the git command is not absolute path to the command and further more my co-worker can run git normally from a Bash script but when invoked directly from the Terminal.

So what gives? Why does the automator workflow work for me but not for my co-worker?

There must be something fundamental about Bash or Unix that I'm not understanding here but I'm lost.

6 Answers 6

60

Scripts run via Automator use the default search path which usually does not include /usr/local/bin. In your case an easy fix would be to put

export PATH=/usr/local/bin:$PATH

somewhere at the beginning of the script.

PS: People using Homebrew on an Apple Silicon Mac should also add

export PATH=/opt/homebrew/bin:$PATH
0
21

For a more general solution to the bash environment in automator differing from your own you could simply load your personal bash profile at the first line of the automator bash script:

source ~/.bash_profile

This will make the path and any other environment variables you're used to using available from your automator script.

5
  • I much prefer this solution. A good overview of the bash login scripts is here
    – Jay
    Commented Jun 18, 2016 at 22:40
  • Struggling to make this work for zsh, source ~/.zprofile didn't help
    – malhal
    Commented May 18, 2021 at 11:37
  • 1
    I had to use source ~/.zshrc Commented Sep 13, 2021 at 17:53
  • 1
    I think this is much better than just exporting $PATH, because there may be many more settings in your bash profile, so the same script may behave differently in Automator vs. in Terminal without source ~/.bash_profile or source ~/.bashrc. Commented Mar 15, 2022 at 10:13
  • This won't actually add /urs/local/bin to your path unless for some reason you've added it to your path in .bash_profile. On macOS, /usr/local/bin typically gets added to your path because it's included as a line in /etc/paths. Commented Sep 22, 2022 at 16:39
7

I solved the same problem of the same "service" workflows being run on differently configured machines by checking what happens when the terminal starts the shell and eventually adding the following snippet to the top of all my "Run Shell Script" actions:

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi
if  [ -f "$HOME"/.profile ]; then
    source "$HOME"/.profile
elif [ -f "$HOME"/.bash_profile ]; then
    source "$HOME"/.bash_profile
elif [ -f "$HOME"/.bashrc ]; then
    source "$HOME"/.bashrc
fi

This covers all the cases I've encountered so far.

2
  • The only answer that works for me under all cases. Thank you 🙇
    – Kim Stacks
    Commented Oct 31, 2021 at 5:21
  • Since Apple doesn't like bash anymore these days, this answer should probably be updated for the shell, which is currently en vogue at Apple. Need to find time to do this (I still use bash)
    – silverdr
    Commented Aug 5, 2023 at 20:20
3

I don't know why any of the other answers were not working for me, but if you use homebrew then put this at the start of the Automator script.

export PATH=/opt/homebrew/bin:$PATH
1
  • 3
    Because you are using an M1 Mac where Homebrew installs into /opt/homebrew instead of /usr/local.
    – nohillside
    Commented Jul 31, 2022 at 17:22
2

If you want the Workflow to work on both machines that have git in different locations, then add each location to the PATH variable that patrix mentioned, separated by a :, as explained here: https://developer.apple.com/library/mac/documentation/AppleApplications/Conceptual/AutomatorConcepts/Articles/ShellScriptActions.html

So, at the very top of your Workflow Shell Script, you would add:

PATH=/usr/bin:/usr/local/bin
export PATH
0

I am using Anaconda so this is what I do with Automator:

/Users/HOMEFOLDER/anaconda3/bin/python <<EOF import sys print("RUN any ANACONA SCRIPT HERE!")

EOF

And I replace the print with my actual code. This works with Siri Shortcuts to which can be controlled with Shortery for full automation

1
  • 1
    It's not a good idea to post duplicate answers. You can definitely refer back to an existing answer, but you should write the answer so it's within the question's context.
    – Allan
    Commented May 21 at 22:58

You must log in to answer this question.

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