我编写了一个小代码片段来检查aws cli版本。
#!/bin/bash
if [ -e "/usr/local/bin/aws" ];
then
myAWS="/usr/local/bin/aws"
else
myAWS="/usr/bin/aws"
fi
myCmd=("${myAWS} --version")
echo "$myCmd"
message=$($myCmd)
echo "$message"现在,当使用root用户手动运行时,我可以运行,但是在通过crontab运行时,我们的aws升级了。
57 21 * * * /rough/scripts/log/test.sh > /rough/scripts/log/test.log 2>&1 我面对下面的错误,你能建议,我已经重新安装了aws cli,但没有任何效果。
/usr/local/bin/aws --version
Traceback (most recent call last):
File "aws", line 19, in
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "PyInstaller/loader/pyimod02_importers.py", line 493, in exec_module
File "awscli/clidriver.py", line 43, in
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "PyInstaller/loader/pyimod02_importers.py", line 493, in exec_module
File "awscli/help.py", line 20, in
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "PyInstaller/loader/pyimod02_importers.py", line 493, in exec_module
File "docutils/core.py", line 23, in
File "", line 1007, in _find_and_load
File "", line 986, in _find_and_load_unlocked
File "", line 680, in _load_unlocked
File "PyInstaller/loader/pyimod02_importers.py", line 493, in exec_module
File "docutils/io.py", line 43, in
UnicodeEncodeError: 'utf-8' codec can't encode characters in position 4-6: surrogates not allowed
[24036] Failed to execute script 'aws' due to unhandled exception!发布于 2023-03-11 15:06:27
克伦也有自己的PATH是在源代码中硬编码的。这意味着cron命令将在不同的环境中运行。我的猜测是,您的用户(或根用户)在其python中配置了不同的PATH。或者您可能在登录时激活了python环境,或者已经安装了一些pip包。无论情况如何,您都需要在您的cron中复制它。
首先,在作为命令所针对的用户登录时运行echo "$PATH"。然后,将打印的PATH添加到crontab的开头:
PATH="blah;blah;blah"
57 21 * * * /rough/scripts/log/test.sh > /rough/scripts/log/test.log 2>&1 如果不起作用,可以尝试使用bash -i加载用户的shell配置文件:
-i If the -i option is present, the shell is interactive.这将迫使bash以“交互式”模式运行,这意味着它将读取调用用户的~/.bashrc文件(参见https://askubuntu.com/a/438170/85695):
57 21 * * * bash -i /rough/scripts/log/test.sh > /rough/scripts/log/test.log 2>&1 如果这仍然不起作用,那么您将需要确切地找出您正在使用的python设置以及它的来源,并尝试在crontab中复制它。如果你用更多关于你的设置的细节来编辑你的问题,我们也许能帮上忙。
在不相关的情况下,您可以稍微简化一下脚本。首先,由于/usr/local/bin和/usr/bin都处于默认的PATH中,如果if的目标是查找其中哪一个包含aws可执行文件,那么您根本不需要if,只需运行aws。
如果您希望始终对/usr/local/bin中的版本进行优先级排序,即使在/usr/bin中还有其他版本,那么if也是有意义的。
接下来,运行message=$(command); echo $message没有多大意义:如果command打印出您想要看到的内容,只需直接运行它。我会像这样写你的剧本:
#!/bin/bash
myAws=/usr/bin/aws
[ -e /usr/local/bin/aws ] && myAws=/usr/local/bin/aws
myCmd=("$myAWS" "--version")
echo "${myCmd[@]}"
"${myCmd[@]}"或者,如果您只想运行一个aws,而if只是想知道它是在哪里安装的,并且您还想看到它的完整路径,那么只需:
#!/bin/bash
myAws=$(which aws)
myCmd=("$myAws" "--version")
echo "${myCmd[@]}"
"${myCmd[@]}"https://askubuntu.com/questions/1458745
复制相似问题