Error Trying to run a Shell Script

I am trying to create a Macro to run a shell script on a schedule. The script runs a node file which imports data from Zotero into an indexed directory monitored by Devonthink.

This is the script, which is on my desktop in the devonthink-master directory:


cd "${0%/*}"
npm start

This is the script I have attempted to run:

The script works fine when run on its own from Terminal. But when I try to run the script from KM I get an error saying npm is not found.

Any thoughts on what I am doing wrong?

Even though I may not have the answer you want, I have an important observation. Your first action does nothing. I imagine you are trying to use the cd command and you think it will carry over into the next action. That won't work. Actually, I haven't tested it, but I am confident in saying that KM actions don't have a memory like that. So that's one error.

You might fix the problem if you put the cd command into the .sh file directly. But what's the point of that since the first item in your script file is another cd. So my question for you is what are you trying to do with that first action?

I think the real problem is this. Your npm program's folder isn't in your PATH variable. Don't you agree that it needs to be there? You can debug this by inserting a statement in your .sh file that displays the path. Then your error will show you what your PATH includes and whether it's missing the npm program's path.

One other thing that helps in a case like this is to notice the cogwheel. If you pull down the cogwheel menu, there's a flag for including errors. Sometimes that provides important information.

That seems like it may be the correct answer. I need to do some research on setting the PATH variable.

Then again - why would it work from Terminal if the PATH variable is not set as needed. What is different about running it from KM?

I actually don't know if the default path from a terminal is also the default path for an Execute Shell Script action. You should check both and compare results. I'm just an average guy here, not one of the elite wizards who knows everything. :slight_smile: There's a fair chance this is the cause of your problem.

I would speculate that you manually edited the path in your Terminal window but may have forgotten about that change. And we can see from your action that you didn't edit the path to include the path for npm. This sort of thing has happened to be before. In fact this is the reason I hate using external commands, because every time an app updates its version number it may update the pathname for its executable, resulting in me have to change my macros.

Shell scripts are run inside a subshell, and each subshell has its own concept of what the current directory is. The cd succeeds, but as soon as the subshell exits, you're back in the interactive shell and nothing ever changed there.

I'm not sure but I just ran the env command from my Terminal window and from an Execute Shell Script action and I examined the PATH variables which were different so definitely there is something different about them. I maintain my hypothesis that your problem lies there.

Agreed - this fixes the issue - thank you.

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

Yay! I helped! I feel great.

1 Like

The working directory (which is what cd changes) is a property of a script (actually it is an environment variable PWD), which is passed down to subscripts, but does not affect the parent.

So you need to combine your two scripts:

cd /whatever

In addition, the PATH (also an environment variable) is different in Keyboard Maestro and in Terminal. Typically in Terminal is is configured by your shell rc scripts (eg .bashrc), but these are not run when you execute a script from Keyboard Maestro.

You can set the PATH explicitly in the script, or you can set the PATH you want to use globally in scripts within Keyboard Maestro by setting the ENV_PATH variable in Keyboard Maestro.

If you select Help from the gear menu in the action, it will take you to the Execute a Shell Script action page which includes details about all of this and more. The Path in Shell Scripts section should be particularly helpful.

1 Like

After you have changed directories, do you still need to prefix the command with the directory path? Wouldn't this work:

cd /whatever

if "dostuff" is in the "whatever" directory.

Yes, you still need the prefix. Or more accurately, maybe.

It depends if your PATH includes "." as an option. Which is generally not a good idea for security reasons.

You can do this:

cd /whatever
1 Like