問題描述
我正在嘗試運(yùn)行以下 bash 腳本,該腳本在激活 conda 環(huán)境后運(yùn)行 Python 程序.
send.bash
#!/bin/bash源激活 manage_oam_userspython ~/path/to/script/send.py源停用
crontab
30 * * * * source/path/to/script/send.bash
我從 cron 收到以下錯誤,盡管運(yùn)行 source send.bash
可以完美運(yùn)行.我也嘗試過使用 bash send.bash
,它在手動運(yùn)行時工作正常,但在從 cron 運(yùn)行時會導(dǎo)致相同的錯誤.
/path/to/script/send.bash: line 2: activate: No such file or directory
activate
和 deactivate
可能是位于 $PATH
中某個條目的腳本代碼>變量指向.通常,為一位用戶在本地安裝的軟件會將語句添加到您的 .profile
文件或 .bashrc
以擴(kuò)展您的 $PATH
變量,以便您可以使用不使用完整路徑的軟件腳本.
當(dāng)您的 bash 自動加載 .profile
和 .bashrc
時,CRON 不會這樣做.對此至少有兩種解決方案.
A) 無處不在的完整路徑
您可以在 CRON 作業(yè)執(zhí)行的腳本中使用完整路徑,如下所示:
#!/bin/bash源/path/to/activate manage_oam_userspython $HOME/path/to/script/send.py源/路徑/到/停用
也使用 $HOME
代替 ~
.您可以在 shell 中使用 which activate
和 which deactivate
找出完整路徑.
B) 源碼.profile
或.bashrc
或者,您可以獲取 .profile
(或 .bashrc
;您必須查看哪個文件擴(kuò)展了您的 $PATH
變量anaconda 目錄)在您的 CRON 選項(xiàng)卡中:
30 * * * * source $HOME/.profile;源/path/to/script/send.bash
補(bǔ)充:來源是什么意思?
<塊引用>source 是一個 Unix 命令,它評估命令后面的文件,作為在當(dāng)前上下文中執(zhí)行的命令列表.
– 來自維基百科,很棒的百科全書
source
命令的常用別名是單點(diǎn) (./path/to/script
).
相關(guān)但更多通用問題可以在 UNIX 和 Linux Stack Exchange 上找到.
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
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
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.
While your bash loads .profile
and .bashrc
automatically, CRON won't do that. There are at least two solutions for this.
A) Full Paths everywhere
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
Also use $HOME
instead of ~
. You can find out the full paths using which activate
and which deactivate
in your shell.
B) Source .profile
or .bashrc
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 is a Unix command that evaluates the file following the command, as a list of commands, executed in the current context.
– from Wikipedia, the something something great encyclopaedia
A commonly used alias for the source
command is a single dot (. /path/to/script
).
A related, but more generic question can be found on the UNIX and Linux Stack Exchange.
這篇關(guān)于從 cronjob 運(yùn)行 bash 腳本失敗,并顯示“沒有這樣的文件或目錄".的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!