-1

I have some console utility installed. It's in /usr/local/bin/my_utility. I can call it as my_utility and it works well. I've created a cron task which runs a bash script. That bash script calls my_utility. The error which occurs only when I can the bash script via cron is "my_utility: command not found".

Why isn't it found? Running the bash script directly works well. Also, the script uses other env. variables with no failure.

Joddy
  • 69
  • 5

1 Answers1

1

The system looks through the directories in PATH to find executables if you don't specify the full path. As such this is almost certainly because the PATH variable used by cron does not include /usr/local/bin/.

It's also possible that the utility is a script, and is not specifying the full path to its interpreter on the first (#!) line. If the path to this interpreter is in your PATH when logged in, but not in cron's PATH then you'd get a similar issue.

Generally speaking it's considered good practice to always use the full path in scripts to be run from cron, as cron may well have a different setting for PATH to you. The alternative is to call your script from cron as /full/location/of/script, and set a new value for PATH in the script.

We get this issue pop up a lot on FreeBSD because cron does not search under /usr/local by default, whereas most (if not all?) Linux distributions do.

USD Matt
  • 5,321
  • 14
  • 23
  • `As such this is almost certainly because the PATH variable used by cron does not include /usr/local/bin/` -- why not? Why am I able to it as `my_utility` without specifing a full path? – Joddy Nov 16 '17 at 08:15
  • I covered this in the answer. The system searches the directories listed in the `PATH` environmental variable when you try to run a command. It's perfectly possible for the shell used by cron to have a different setting for `PATH` to you. – USD Matt Nov 16 '17 at 12:44