bash 在屏幕上打印消息并同时发送到系统日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18571360/
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
Printing message on the screen and also send to the syslog at the same time
提问by MacUsers
I'm trying to implement -s
(i.e. silent) option in my script - when given the Errors/Info etc, will be send to the syslog otherwise printing on the screen and also sending to the syslog
at the same time. That's what I'm doing:
我正在尝试-s
在我的脚本中实现(即静默)选项 - 当给出错误/信息等时,将发送到系统日志,否则会在屏幕上打印并同时发送到系统日志syslog
。这就是我正在做的:
echo -e "This Is a Test Message\nWell, not really!!" 2>&1 | logger
to send the echo
message to the syslog
(which doesn't print on-screen) but couldn't just figure out how to do the both at the same time. I see people only talk about either logging with syslog
or sending log to a different filewhilst printing on the screen but not the situation that I'm trying deal with. Any help or pointer would be greatly appreciated. Cheers!!
将echo
消息发送到syslog
(不打印在屏幕上)但无法同时弄清楚如何同时执行这两项操作。我看到人们只谈论在屏幕上打印时记录syslog
或发送日志到不同的文件,而不是我正在尝试处理的情况。任何帮助或指针将不胜感激。干杯!!
采纳答案by konsolebox
If you want to send the message to syslog and to stdout (not stderr), you can do this too:
如果您想将消息发送到 syslog 和 stdout(不是 stderr),您也可以这样做:
echo -e "This Is a Test Message\nWell, not really!!" | tee >(exec logger)
And the efficient way to do it (if you're creating a function):
以及执行此操作的有效方法(如果您正在创建一个函数):
exec 40> >(exec logger)
function log {
echo -e ""
echo -e "" >&40
}
log "something..."
exec 40>&- ## optionally close it at the end of the script.
That would save your script from calling external binary logger everytime you do an echo.
这样可以避免每次执行回显时调用外部二进制记录器的脚本。
回答by David Sainty
In that case, just echo the same message twice. You can do:
在这种情况下,只需回显相同的消息两次。你可以做:
echo -e "This Is a Test Message\nWell, not really!!" | tee /dev/stderr | logger
But, it's actually simpler and more efficient to do:
但是,这样做实际上更简单、更有效:
error='This Is a Test Message\nWell, not really!!'
echo -e "$error" >&2
echo -e "$error" | logger
The >&2 sends the output to stderr, rather than stdout.
>&2 将输出发送到 stderr,而不是 stdout。
回答by David Sainty
You can do:
你可以做:
echo -e "This Is a Test Message\nWell, not really!!" | logger -s
The manual page (man logger) states:
手册页(man logger)指出:
-s Log the message to standard error, as well as the system log.