使用 cron 运行脚本的正确方法? [英] Proper way to run a script using cron?
问题描述
使用 cron 运行脚本时,内部调用的任何可执行文件都必须具有完整路径.我发现这试图运行 wondershaper,当它尝试调用 tc 时显示许多错误.所以我的问题是,克服这个问题的正确方法是什么?
When running a script with cron, any executable called inside must have the full path. I discovered this trying to run wondershaper, when many errors showed when it tried to call tc. So my question is, what's the proper way to overcome this problem?
可能的解决方案:
- cd 到可执行文件夹并准备符号链接到那里的任何其他调用的可执行文件(不确定它是否有效 - 低可移植性)
- 在脚本中使用完整路径(它有效 - 跨不同发行版的低可移植性)
- 使用脚本中所需的路径导出路径变量(不确定它是否有效)
好吧,提前感谢任何人的帮助.
Well, thanks in advance for anyone helping.
推荐答案
在 cron 作业中声明变量更明确且更易于维护:您需要修改的所有内容都包含在您的 cron 作业中,您不需要如果您将其移动到另一个系统,则可以传输多个文件.
Declaring variables inside your cron job is more explicit and easier to maintain : all you have to modify is contained in your cron job, and you don't need to transfer multiple files should you move it to another system.
PATH=/usr/bin:/your/fancy/dir
MYAPPROOT=/var/lib/myapp
*/2 * * * * myappinpath
*/3 * * * * $MYAPPROOT/mylocalapp
这篇关于使用 cron 运行脚本的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!