Running Grunt from OSX Automator shell script - macos

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:
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

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

Related

Why do I get different results when launching a shell script from automator or AppleScript than when launching from the terminal?

I have a simple script to launch an electron app using NPM.
npm start --prefix /Users/thinkdigital/development/github/ytmdesktop/ &
disown %$(jobs | grep ytmdesktop | cut -c 2)
Since I want to save this in the dock as a launcher, I tried running this process from many different places.
A Dart script
Automator
Apple Script
Each time it runs the command, other than from the shell, it doesn't launch the application. In fact, when launching from Dart, I do get this in stderr.
Cannot read property 'app' of undefined
at new ElectronStore....
Is there something I need to do to get this script to function when launched from anywhere except directly through the terminal?
I can't speak to Dart, but you should be aware that do shell script in AppleScript and Run Shell Script actions in Automator workflows don't load the configuration files that normally get loaded when you open a shell in Terminal. In particular, the default PATH variable is stripped down — "/usr/bin:/bin:/usr/sbin:/sbin" — and doesn't include "/usr/local/bin" where npm is located (assuming it's still in the default location; run which npm in Terminal to make sure).
Try using the full path to npm and see if that solves your problem:
/usr/local/bin/npm start --prefix /Users/thinkdigital/development/github/ytmdesktop/ & disown %$(jobs | grep ytmdesktop | cut -c 2)

When program uses Command to open Bash to run a script, Bash closes immediately without running the script

I have a simple, already-working bash script set up to launch specific files with specific programs in the gaming frontend EmulationStation on Windows.
But the frontend routes its actions through a Command Prompt. And when Command is used to run the script through Bash, the Bash shell just opens and then closes immediately.
Here's an image of what shows for the instant before Bash closes.
This is only happening when going through a separate Command Prompt first, such as Windows Command Prompt or Git Command Prompt. Running the script with an appropriate argument directly through the git-bash shell works just fine.
In case you want to see the script for any reason, here it is:
#!/bin/bash
defaultemulaunch="V:/Emulation/.emulationstation/systems/retroarch/retroarch.exe -L "V:/Emulation/.emulationstation/systems/retroarch/cores/bsnes_mercury_accuracy_libretro.dll" \"$1\""
emu1names=(\
"(1999) Fire Emblem - Thracia 776.smc")
emu1launch="V:/Emulation/.emulationstation/systems/retroarch/retroarch.exe -L "V:/Emulation/.emulationstation/systems/retroarch/cores/snes9x_libretro.dll" \"$1\""
gamename=`basename "$1"`
for index in ${!emu1names[*]}
do
game=${emu1names[index]}
if [ "$game" == "$gamename" ]; then
eval "$emu1launch"
fi
done
eval "$defaultemulaunch"
But it's worth pointing out that this is happening when trying to run any bash script when starting the process from a separate Command Prompt.
Note: Git is installed on the hard drive that houses the emulation frontend (V:)---not in the user directory or programs directory of the system's OS/boot drive (C:). I mention this because git-bash's failure at an apparent "login" step except when launched directly feels like it could be a default filepath issue.
Check if that program would still open/close a Windows when executed from the CMD with:
bash -c '/v/path/to/bash/script'
In your case:
set PATH=V:\Emulation\
set GIT_HOME=V:\Emulation\Git
set PATH=%GIT_HOME%;%GIT_HOME%\bin;%GIT_HOME%\usr\bin;%GIT_HOME%\mingw64\bin;%PATH
Then:
cd V:/Emulation/.emulationstation/roms/snes/
bash -c './gamelaunch.sh "./(1990) F-Zero.sfc"'
I usually make a run.bat script which would:
set the correct PATH
launch the correct script
That way, for any of my project, I just type run.
And it runs.

Launch meteor with a shell script

I have no trouble starting my meteor server from the terminal window. But surely I can do this from a shell script that I make with Automator. This is what I do in the terminal window:
cd myAppName
meteor
For the the automator shell script, I change the directory differently:
cd /Users/myName/myAppName
... and that seems to work fine. But the next line gives a "meteor --command not found" message. I read somewhere that the shell that automator runs doesn't have access to the Environment variables that the terminal window uses. That must be the problem. So how do I run meteor from the shell script?
Try setting the PATH variable from inside your script. It should include the directory path to your meteor executable.
It should be something like:
export PATH=$PATH:/opt/meteor/bin/meteor
After that meteor should launch. If meteor starts launching but it fails then you're missing more environment variables inside your script and you must export them as you did with the PATH.
To know which environment variables you have in your shell type this:
set

Script shell to automate deployment site

I'm trying to write a script shell (Mac OS X) for deploying my Awestruct site within a launch agent.
So, I have to generate site before deploying it. The command to generate site is :
awestruct -g
My script is myscript.sh :
cd /my/site/structure/base/directory
awestruct -g
This script is launched by the system when an event occurs.
But the problem is it doesn't know about awestruct...
My awestruct program is a part of my ruby installation and is added to my PATH variable.
When I run the script manually, it works (because it's my user who is lauynching it)
When the system detect the event and runs the script, it results as :
awestruct: command not found
The problem is the PATH...
If it works manually, then in the same prompt where it works, run command:
which awestruct
That will print the program file with full path, let's assume /usr/local/bin/awestruct, but use whatever it really is. Then use that in your script, for example:
cd /my/site/structure/base/directory
/usr/local/bin/awestruct -g
This assumes that there are no other environment variables, only defined for your account, which awestruct needs. If there are, then find out what they are, and add them to your script before running awestruct, for example with line:
export AWESTRUCT_ENVIRONMENT_VARIABLE=foobar
(Note: When you run the script normally like any program, that will not change the parent shell environment.)
You can also add the path to the executable in the PATH of the user that run the script and gets the error.
You could try something like :
$ su - <user_that_run_the_script>
$ echo "export PATH=$PATH:$(which awestruct)" >> ~/.bash_profile
$ source ~/.bash_profile
(For Linux users, use ~/.bashrc instead of ~/.bash_profile)

Running shell commands from Xcode breakpoint

I'm trying to run a shell script from a breakpoint in Xcode 4.5 DP 3. I set the breakpoint as such using the Choose button.
However, upon hitting the breakpoint I get the following message:
Error in shell command for breakpoint "(selector name)". The command "/Users/Max/Developer/saveToLog.sh" does not exist.
I've tried to put regular shell commands, like "say test" but Xcode prints the same message (with a different command name, of course). The shell script works fine when I run it from terminal.
Ok, first check to make sure you have the shell permissions set correctly. In terminal type:
chmod u+x saveToLog.sh
And I would also locate the shell script inside the project folder (didn't seem to work correctly for me when the script was located on my desktop).
This is the script I tested on:
#!/bin/bash
touch ~/Desktop/ItWorks.txt
echo "This actually works!" > ~/Desktop/ItWorks.txt

Resources