问题描述
我正在尝试运行以下bash脚本,该脚本在激活conda环境后运行Python程序.
I'm trying to run the following bash script which runs a Python program after activating a conda environment.
send.bash
#!/bin/bash
source activate manage_oam_users
python ~/path/to/script/send.py
source deactivate
crontab
30 * * * * source /path/to/script/send.bash
我从cron收到以下错误,尽管运行 source send.bash
效果很好.我还尝试过使用 bash send.bash
进行手动运行时效果很好,但是从cron运行时会导致相同的错误.
I get the following error from cron, although running source send.bash
works perfectly. I've also tried using bash send.bash
which works fine when run manually, but results in the same error when run from cron.
/path/to/script/send.bash: line 2: activate: No such file or directory
推荐答案
activate
和 deactivate
可能是位于 $ PATH 条目中的脚本.代码>变量指向.通常,为一个用户在本地安装的软件会将语句添加到您的
.profile
文件或 .bashrc
中,以扩展您的 $ PATH
变量,以便您可以使用软件的脚本,而无需使用完整路径.
activate
and deactivate
are probably scripts located somewhere an entry in your $PATH
variable points to. Usually, software installed locally for one user adds statements to your .profile
file or .bashrc
that extend your $PATH
variable so that you can use the software's scripts without using full paths.
当bash自动加载 .profile
和 .bashrc
时,CRON不会这样做.至少有两个解决方案.
While your bash loads .profile
and .bashrc
automatically, CRON won't do that. There are at least two solutions for this.
在CRON作业执行的脚本中使用完整路径,如下所示:
Either you use full paths in the script executed by your CRON job, like this:
#!/bin/bash
source /path/to/activate manage_oam_users
python $HOME/path/to/script/send.py
source /path/to/deactivate
还使用 $ HOME
代替〜
.您可以在外壳程序中使用 activate
和停用
来查找完整路径.
Also use $HOME
instead of ~
. You can find out the full paths using which activate
and which deactivate
in your shell.
或者,您可以获取您的 .profile
(或 .bashrc
);您将必须查找哪个文件扩展了您的 $ PATH
变量,anaconda目录)在您的CRON标签中:
Alternatively you can source your .profile
(or .bashrc
; you will have to look which file extends your $PATH
variable with the anaconda directories) in your CRON tab:
30 * * * * source $HOME/.profile; source /path/to/script/send.bash
额外:来源是什么意思?
Extra: What does source mean?
–维基百科的那是很棒的百科全书
source
命令的常用别名是单个点(./path/to/script
).
A commonly used alias for the source
command is a single dot (. /path/to/script
).
与之相关,但更多通用问题可以在UNIX和Linux Stack Exchange上找到.
这篇关于从cronjob运行bash脚本失败,并显示“无此文件或目录".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!