使用cron运行脚本的正确方法? [英] Proper way to run a script using cron?

查看:196
本文介绍了使用cron运行脚本的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用cron运行脚本时,任何内部调用的可执行文件都必须具有完整路径。我发现这尝试运行奇思妙想,当尝试调用tc时出现许多错误。所以我的问题是,解决这个问题的正确方法是什么?



可能的解决方案:




  • cd到可执行文件夹并准备到任何其他被调用的可执行文件的符号链接(不确定是否可行 - 低可移植性)

  • 在脚本中使用完整路径在不同发行版之间的低可移植性)

  • 在脚本中导出包含所需路径的路径变量(不确定是否可以使用)

$ b $

解决方案

声明cron作业中的变量更多显式和更易于维护:您需要修改的所有内容都包含在您的cron作业中,如果您将其移动到另一个系统,则不需要传输多个文件。

  PATH = / usr / bin:/ your / fancy / dir 
MYAPPROOT = / var / lib / myapp

* / 2 * * * myappinpath
* / 3 * * * * $ MYAPPROOT / mylocalapp


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?

Possible solutions:

  • cd to the executable folder and prepare symbolic links to any other called executable there (not sure if it works - low portability)
  • use full paths in the script (it works - low portability across different distros)
  • exporting a path variable with the needed paths inside the script (not sure if it works)

Well, thanks in advance for anyone helping.

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆