Linux 测试运行cron入口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8132355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Test run cron entry
提问by Karthick S
I added a cron job recently, but made a mistake in the path while giving the command and hence, the job never succeeded. Is there some way to test the cron changes we have done?
我最近添加了一个 cron 作业,但是在发出命令时在路径中犯了一个错误,因此该作业从未成功。有什么方法可以测试我们所做的 cron 更改吗?
Please note that I had indeed copied and pasted the command from my command line and it was just an stray keypress that caused this.
请注意,我确实从命令行复制并粘贴了该命令,这只是导致此问题的一个杂散按键。
采纳答案by Joshua
This question has also been asked on serverfault and has garnered a couple additional answers
在serverfault上也有人问过这个问题,并获得了一些额外的答案
The following is a paraphrased version of Marco's solution: (Not sure if best ediquite is not providing a link only answer or not copying someone else's solution)
以下是Marco 解决方案的释义版本:(不确定最好的 ediquite 是不提供仅链接的答案还是不复制其他人的解决方案)
Create a environment file with a temporary cron entry
使用临时 cron 条目创建环境文件
* * * * * /usr/bin/env > /home/username/cron-env
Then create a shell script called run-as-cron
which executes the command using that environment.
然后创建一个 shell 脚本,调用run-as-cron
它使用该环境执行命令。
#!/bin/sh
. ""
exec /usr/bin/env -i "$SHELL" -c ". ; "
Give it execute permission
给它执行权限
chmod +x run-as-cron
and then it is then used like this:
然后像这样使用它:
./run-as-cron <cron-environment> <command>
e.g.
例如
./run-as-cron /home/username/cron-env 'echo $PATH'
回答by Martin Olsen
When I want to test my cron jobs I usually set the interval very low and monitor the logs closely. When I am convinced the entry is correct, I set the interval back to a sane value.
当我想测试我的 cron 作业时,我通常将间隔设置得非常低并密切监视日志。当我确信输入是正确的时,我将间隔设置回一个合理的值。
For example, run job every two minutes:
例如,每两分钟运行一次作业:
*/2 * * * * echo "Hello World"
*/2 * * * * echo "Hello World"
And the I run tail -f
on my log file (/var/log/syslog
on debian).
我tail -f
在我的日志文件/var/log/syslog
上运行(在 debian 上)。
回答by Fuujuhi
Joshua's answer does not work for me. Two problems:
约书亚的回答对我不起作用。两个问题:
Variables in
cron-env
file are not exported (set -a
needed).Script is still tied to current tty (
setsid
needed).
cron-env
文件中的变量不会导出(set -a
需要)。脚本仍然绑定到当前的 tty(
setsid
需要)。
The script run-as-cron
should be
脚本run-as-cron
应该是
#!/bin/sh
. ""
exec setsid /usr/bin/env -i "$SHELL" -c "set -a; . ; " </dev/null
Not enough rep' to fix his answer or add a comment...
没有足够的代表来修复他的答案或添加评论......
回答by Amit Bhat
use command crontab -e This will open a vim editor and all you got to do here is * * * * * /somepath/urscript.sh , make sure you have the appropriate spaces between dates and the path of the script After the execution , you can check in the /var/spool/mail there will a complete trail of the script execution or errors. For testing there is no way .. but in case ur sh urscript.sh works then cron tab will have no problem as it is exactly same thing what u do manually.
使用命令 crontab -e 这将打开一个 vim 编辑器,你在这里要做的就是 * * * * * /somepath/urscript.sh,确保日期和脚本路径之间有适当的空格 执行后,您可以在 /var/spool/mail 中查看脚本执行或错误的完整踪迹。对于测试,没有办法..但如果你的 sh urscript.sh 工作,那么 cron 选项卡将没有问题,因为它与你手动执行的完全相同。