crontab 源文件
最近我创建了一个 bash 脚本,我应该在 cron 中运行它。
准备好bash脚本并正常工作后,我将其放入Cron中,发现它失败了。作为第二步,我删除了所有环境依赖项,即不仅仅是 file.txt
,我指定了 /home/blah-blah/file.txt
我仍然找到了脚本仍一步步失败。该步骤是一个数据处理工具。 我执行的命令是 /bin/blah-blah/processing_tool -parameter $INDEX
其中 $INDEX
是 bash 脚本中计算的变量。
第三步是将 bash 配置文件添加为 bash 脚本开头的源。瞧!!!!该脚本从 cron 开始完美执行。
我的问题是,即使我从脚本中删除了所有环境依赖项,为什么还会发生这种情况。我还听说不建议将 cron 作业外包给 bash 配置文件。如果是这样,有没有其他方法可以避免这样做。
Recently I created a bash script which I am supposed to run in cron.
After preparing the bash script and its normal working, I put it in Cron and found that it was failing. As as second step , I removed all the environment dependencies i.e instead of just file.txt
, I specified /home/blah-blah/file.txt
I still found the script to be failing still at one step. The step was a data processing tool.
The command i executed was /bin/blah-blah/processing_tool -parameter $INDEX
where $INDEX
is a variable calculated within the bash script.
Third step was to add the bash profile as source at the beginning of the bash script. Voila!!!! The script started executing perfectly from cron.
My question is why is this happening even after I removed all the environment dependencies from my script. Also I have heard that sourcing a cron job to a bash profile is not recommended. If so, Is there any other way in which I can avoid doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本上:任何从 cron 开始的事情都是从一个完全干净的状态开始的。
您不能对环境变量的内容或从 cron 运行的任何脚本开始时的当前文件夹做出任何假设。
最简单的解决方案:
cd 到所需的目录以确保您的路径位于所需的位置。
source /etc/profile 以确保您获得系统范围的环境变量设置。
source ~myuserid/.profile 来读取您的个人环境设置。 (~/.profile 不起作用,因为它指示 cron 用户。)
然后开始执行实际的脚本。
当然,上面的方法需要 cron 进程对您的主目录具有读取权限,并且它可能会执行实际需要的更多工作。
稍微复杂一些:找出脚本需要哪些环境变量以及脚本调用的任何内容。
在 cron 脚本的开头显式导出这些内容。
(Ps 将 /etc/profile 和 ~myuserid/.profile 替换为您选择的 shell 的相应文件。)
Basicly: Anything started from cron starts with a totally clean slate.
You can make no assumptions whatsoever about the content of environment variables or whichever folder is the current folder at the start of any script run from cron.
Easiest solution:
cd to the desired directory to make sure your path is in the desired location.
source /etc/profile to mak sure you get the system wide environment variables setup.
source ~myuserid/.profile to read your personal environment settings. (~/.profile won't work as that would indicate the cron user.)
Then start executing the actual script.
Of course the approach above requires the cron process to have read access to your home-dir adn it's probably doing a lot more work thatn is actually required.
Slightly more complicated: Figure out which environment variables are required by the script and anything that gets called by the script.
Explicitly export these at the beginning of the cron script.
(P.s. replace /etc/profile and ~myuserid/.profile with whatever are the corresponding files for your shell of choice.)
一个
cron
可以被认为是一个单独的用户。因此,这个“用户”可能不会像您一样“看到”或“读取”相同的文件。因此,所有路径名等都必须以绝对方式定义。每个脚本都在其自己的进程中运行。因此,当您运行脚本时,您可以更改 $SHELL 和其中的任何其他变量,但一旦退出脚本,这些变量就会丢失。我的猜测是,
$INDEX
变量计算可能已在脚本内成功计算,但在脚本外部使用可能会失败。如果没有更多关于这是什么工作或你想做什么的信息,很难说。有两种方法可以运行 cron 作业:
。root crontab
中的作业 >"/home/blah/.profile && myScript"
也就是说,您的环境变量中必须有一些东西(除了文件扩展名),当您运行 cron 作业时不存在。您必须使用
-x 标志(在 bash 中)
执行该脚本,然后仔细研究输出。在环境变量和 root/cron 环境变量之间使用差异可能是一个指针。另外,检查脚本中是否使用了一些实用程序,这些实用程序的位置不属于 cron/root 的$PATH
变量。A
cron
can be thought of as a separate user. So, this "user" may not "see" or "read" the same files as you do. It is thus essential that all path names etc. be defined in the absolute.Every script runs within its own process. So, when you run a script, you can change the
$SHELL
and any other variable within but it will be lost once you get out of it. My guess is that the$INDEX
variable computation may have had been computed within the script successfully but its use outside of the script may have failed. Without more information about what job it was, or what you wanted to do, it is hard to tell.There are two ways to run a cron job:
su -user -c < job >
inroot crontab
."/home/blah/.profile && myScript"
That said, there HAS to be something in your environment variables (apart from file extensions) that is not present when you run the cron job. You will have to execute that script with
-x flag (in bash)
and then pore over the output. Using adiff
between your environment variables and that ofroot/cron
might be a pointer. Also, check if there are some utilities that are being used in your scripts whose locations are not part of the$PATH
variable for cron/root.