Crontab并测试要执行的命令 [英] Crontab and testing a command to be executed

查看:109
本文介绍了Crontab并测试要执行的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对cron和crontab很新。

I'm quite new to cron and crontab.

我编辑了crontab文件,我需要手动执行一个命令,所以我可以尝试它,事先测试。我怎么做?如果失败,是否有一个模式显示错误?

I've edited the crontab file and I need to execute manually one of commands so I can try it and test it beforehand. How do I do that? If it fails, is there a mode that shows the errors?

推荐答案


  1. 编写一个您可以测试的shell脚本。

  2. <
  3. 请记住, cron 几乎不提供任何环境,因此您的脚本可能需要修复。

  4. 不要对您在crontab中放置的内容感兴趣。

  5. 在您的个人资料中构建调试模式shell脚本。

  1. Write a shell script that you can test.
  2. Execute that shell script from the crontab.
  3. Remember that cron provides barely any environment - so your script may have to fix that. In particular, your profile will not be used.
  4. Do not get fancy with what you put in the crontab.
  5. Build a debug mode into your shell script.

不,没有具体的模式显示错误。通常,如果cron作业失效,输出将通过电子邮件发送给您。也就是说,如果执行的命令向标准输出或标准错误写入任何内容,它会向您发送标准输出和标准错误信息。

No, there isn't specifically a mode that shows errors. Usually, if the cron job witters, the output is emailed to you. That is, it sends standard output and standard error information to you if the executed command writes anything to either standard output or standard error.

在MacOS X(10.6.7) ,我得到的环境是(通过 crontab 条目如 12 37 17 5 * env> /tmp/cron.env ):

On MacOS X (10.6.7), the environment I got was (via a crontab entry like 12 37 17 5 * env >/tmp/cron.env):

SHELL=/bin/sh
USER=jleffler
PATH=/usr/bin:/bin
PWD=/Users/jleffler
SHLVL=1
HOME=/Users/jleffler
LOGNAME=jleffler
_=/usr/bin/env

其中, PWD _ SHLVL 由shell处理。因此,要在类似cron的环境中可靠地测试脚本,请使用:

Of those, PWD, _ and SHLVL are handled by the shell. So, to test your script reliably in a cron-like environment, use:

(cd $HOME
 env -i \
    SHELL=/bin/sh \
    USER=$USER \
    PATH=/usr/bin:/bin \
    HOME=$HOME \
    LOGNAME=$LOGNAME \
    /path/to/script/you/execute ...
)

-i 选项 env 表示忽略所有继承环境 ;该脚本将完全看到指定的五个值加上shell自动指定的任何内容。没有参数, env 报告环境;通过参数调整环境并执行命令。

The -i option to env means 'ignore all inherited enviroment'; the script will see exactly the five values specified plus anything the shell specifies automatically. With no arguments, env reports on the environment; with arguments, it adjusts the environment and executes a command.

这篇关于Crontab并测试要执行的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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