Cron has its own PATH
which is hardcoded in the source code. This means that cron commands will run in a different environment. My guess is that your user (or root) are configured with a different python
in their PATH
. Or maybe you activate a python environment on login, or have installed some pip
packages. Whatever the case may be, you will need to reproduce that in your cron
.
First, run echo "$PATH"
while logged in as the user for whom the command works. Then, take the PATH
that prints and add it to the beginning of the crontab:
PATH="blah;blah;blah"
57 21 * * * /rough/scripts/log/test.sh > /rough/scripts/log/test.log 2>&1
If that doesn't work, you can try loading the user's shell configuration file with bash -i
:
-i If the -i option is present, the shell is interactive.
That will force bash
to run in "interactive" mode, meaning it will read the invoking user's ~/.bashrc
file (see https://askubuntu.com/a/438170/85695):
57 21 * * * bash -i /rough/scripts/log/test.sh > /rough/scripts/log/test.log 2>&1
If that still doesn't work, you will need to figure out exactly what python setup you are using and where it comes from and try to reproduce that in the crontab. If you edit your question with more detail about your setup we might be able to help.
On an unrelated note, you could simplify your script a little. First, since both /usr/local/bin
and /usr/bin
are in the default PATH
, if the objective of your if
is to find which of the two contains an aws
executable, then you don't need the if
at all and can simply run aws
.
If instead you want to always prioritize the version in /usr/local/bin
even if there is another one in /usr/bin
, then the if
makes sense.
Next, there's not much point in running message=$(command); echo $message
: if command
prints out what you want to see, just run it directly. I would write your script like this instead:
#!/bin/bash
myAws=/usr/bin/aws
[ -e /usr/local/bin/aws ] && myAws=/usr/local/bin/aws
myCmd=("$myAWS" "--version")
echo "${myCmd[@]}"
"${myCmd[@]}"
Or, if you just want to run an aws
and the if
was only to figure out where it is installed, and you also want to see the full path to it, simply:
#!/bin/bash
myAws=$(which aws)
myCmd=("$myAws" "--version")
echo "${myCmd[@]}"
"${myCmd[@]}"