Linux 如何使用参数运行 cron 作业并将结果传递给日志?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7858114/
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
How can I run a cron job with arguments and pass results to a log?
提问by Ryan
Example:
例子:
* * * * * /usr/bin/php /full/path/to/script.php arg1 arg2 > /full/path/to/logfile.log
The script runs and accesses the arguments just fine, but the results are never printed to the logfile.log. Also, my logfile.log is chmod 777, so I know it has write access.
脚本运行并访问参数很好,但结果永远不会打印到 logfile.log。另外,我的 logfile.log 是 chmod 777,所以我知道它有写权限。
Can you fix my syntax?
你能修复我的语法吗?
采纳答案by mik
It looks like you are searching for the log file in the wrong folder. Try this
看起来您正在错误的文件夹中搜索日志文件。尝试这个
* * * * * cd /path/to/script.php ; ./script.php arg1 arg2 >> logfile.log
Then look for your log file in the /path/to/script folder. It can also be a write permission problem. Also, check your script for errors. Your crontab command seems OK.
然后在 /path/to/script 文件夹中查找您的日志文件。也可能是写权限问题。另外,请检查您的脚本是否有错误。您的 crontab 命令似乎没问题。
回答by Ashwinee K Jha
Does the command work correctly from the command line? It may be possible that the arguments contain some characters treated specially by shell. In that case you need to quote the whole command to prevent shell from tinkering with the arguments.
该命令是否从命令行正常工作?参数可能包含一些由 shell 特殊处理的字符。在这种情况下,您需要引用整个命令以防止 shell 修改参数。