从 cronjob 运行 bash 脚本失败,并显示“没有这样的文件或目录".

Running a bash script from a cronjob fails with quot;No such file or directoryquot;(从 cronjob 运行 bash 脚本失败,并显示“没有这样的文件或目录.)
本文介绍了从 cronjob 运行 bash 脚本失败,并显示“没有这样的文件或目录".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

send.bash

#!/bin/bash源激活 manage_oam_userspython ~/path/to/script/send.py源停用

crontab

30 * * * * source/path/to/script/send.bash

我从 cron 收到以下错误,尽管运行 source send.bash 可以完美运行.我也尝试过使用 bash send.bash,它在手动运行时工作正常,但在从 cron 运行时会导致相同的错误.

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

解决方案

activatedeactivate 可能是位于 $PATH 中某个条目的脚本代码>变量指向.通常,为一位用户在本地安装的软件会将语句添加到您的 .profile 文件或 .bashrc 以扩展您的 $PATH 变量,以便您可以使用不使用完整路径的软件脚本.

当您的 bash 自动加载 .profile.bashrc 时,CRON 不会这样做.对此至少有两种解决方案.

A) 无处不在的完整路径

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

#!/bin/bash源/path/to/activate manage_oam_userspython $HOME/path/to/script/send.py源/路径/到/停用

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

B) 源码.profile.bashrc

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

30 * * * * source $HOME/.profile;源/path/to/script/send.bash

补充:来源是什么意思?

<块引用>

source 是一个 Unix 命令,它评估命令后面的文件,作为在当前上下文中执行的命令列表.

– 来自维基百科,很棒的百科全书

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

相关但更多通用问题可以在 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.

这篇关于从 cronjob 运行 bash 脚本失败,并显示“没有这样的文件或目录".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

python arbitrarily incrementing an iterator inside a loop(python在循环内任意递增迭代器)
Joining a set of ordered-integer yielding Python iterators(加入一组产生 Python 迭代器的有序整数)
Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
What is the Perl version of a Python iterator?(Python 迭代器的 Perl 版本是什么?)
How to create a generator/iterator with the Python C API?(如何使用 Python C API 创建生成器/迭代器?)
Python generator behaviour(Python 生成器行为)