从cronjob运行bash脚本不工作

2024-03-28 13:52:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试运行以下bash脚本,它在激活conda环境后运行Python程序。在

发送.bash

#!/bin/bash
source activate manage_oam_users
python ~/path/to/script/send.py
source deactivate

crontab

^{pr2}$

{cron>尽管下面的错误^运行正常。我还尝试过使用bash send.bash,它在手动运行时运行良好,但在从cron运行时会产生相同的错误。在

/path/to/script/send.bash: line 2: activate: No such file or directory

Tags: topath程序脚本bashsendsourcebin
2条回答

由于cron不是从安装anaconda的目录运行的,因此无法找到activate。而且你的路径似乎缺少根Python目录。 找到activate命令的位置并将其添加到路径中。在

which activate
/Users/username/anaconda/bin/activate

在你的bash_配置文件中添加

^{pr2}$

activatedeactivate可能是位于$PATH变量中某个条目所指向的脚本。通常,为一个用户本地安装的软件会将语句添加到您的.profile文件或.bashrc中,这些语句扩展了$PATH变量,这样您就可以在不使用完整路径的情况下使用软件的脚本。在

当bash自动加载.profile和{}时,CRON不会这样做。至少有两种解决方案。在

A) 到处都是满路

要么在CRON作业执行的脚本中使用完整路径,如下所示:

#!/bin/bash
source /path/to/activate manage_oam_users
python $HOME/path/to/script/send.py
source /path/to/deactivate

也可以使用$HOME代替~。您可以使用shell中的which activatewhich deactivate找到完整的路径。在

B) 源.profile或{}

或者,您可以在CRON选项卡中找到您的.profile(或.bashrc;您必须查看哪个文件用anaconda目录扩展$PATH变量):

^{pr2}$

额外说明:来源是什么意思?在

source is a Unix command that evaluates the file following the command, as a list of commands, executed in the current context.

{a1}

source命令的常用别名是一个单点(. /path/to/script)。在

A related, but more generic question can be found on the UNIX and Linux Stack Exchange.

相关问题 更多 >