简体   繁体   English

Linux:stdout和stderr到socket

[英]Linux: stdout and stderr to socket

I want to redirect stdout and stderr to a socket that I can then use to remotely monitor the status of my application over Ethernet. 我想将stdout和stderr重定向到一个套接字,然后我可以使用该套接字通过以太网远程监控我的应用程序的状态。 Currently I accomplish this by using ssh and watching the output in the shell console. 目前我通过使用ssh并在shell控制台中观察输出来实现此目的。 I'd prefer to remove the middle man if possible and just send the entire stdout and stderr output to a udp or tcp/ip port and have my monitoring computer connect to it. 如果可能的话,我宁愿删除中间人,只需将整个stdout和stderr输出发送到udp或tcp / ip端口,并让我的监控计算机连接到它。

I cannot use UART or any other wired connection. 我不能使用UART或任何其他有线连接。 It has to be Ethernet. 它必须是以太网。 Also, if possible, I'd like to accomplish this via a bash script, to prevent having to rebuild my application. 另外,如果可能的话,我想通过bash脚本完成此操作,以防止重建我的应用程序。

Thanks for the help. 谢谢您的帮助。

The way you describe it, it sounds like your going to need either your existing application to open a passive socket and wait for connections, or your going to have to wrap your application in something that sets up a listening socket. 你描述它的方式,听起来你需要你现有的应用程序打开一个被动套接字并等待连接,或者你必须将你的应用程序包装在一个设置监听套接字的东西中。 This post suggests that is not possible in just Bash, however it does show ways to do it from the command line with netcat or perl. 这篇文章表明,在Bash中是不可能的,但它确实显示了使用netcat或perl从命令行执行此操作的方法。 For example you could do something like this with netcat: nc -l -p <port> -c "tail -F /var/log/blah" 例如,您可以使用netcat执行类似的操作: nc -l -p <port> -c "tail -F /var/log/blah"

On the monitored application side, there is a way to redirect both outputs to an outbound connection, using netcat: 在受监视的应用程序端,有一种方法可以使用netcat将两个输出重定向到出站连接:

$ ./application 2>&1 | nc <remote-host> <remote-port>

This way, you're redirecting stderr to stdout and then pipe it all together to netcat, which will take care of setting up the socket, establish connection with the remote host and all that stuff. 这样,你将stderr重定向到stdout ,然后将它们一起管道到netcat,这将负责设置套接字,建立与远程主机的连接以及所有这些东西。

However, bear in mind that you can suffer from printf() 's buffering, if that's the function you're using to write to stdout . 但是,请记住,如果这是你用来写入stdout的函数,你可能会受到printf()的缓冲。 In my local tests, I've seen that the data sent to stderr by the application is seen immediately on the other listening end, but on the other hand the data sent to stdout is only sent when the application exits or there's enough data in the buffer to flush it all at once. 在我的本地测试中,我已经看到应用程序发送到stderr的数据立即在另一个监听端看到,但另一方面,发送到stdout的数据仅在应用程序退出时发送或者在应用程序中有足够的数据缓冲区一次冲洗它。 So, if you care about the order and the availability of the info on the monitoring side, I'd suggest you to place calls to fflush(stdout); 所以,如果您关心监控端的信息顺序和可用性,我建议您拨打fflush(stdout); whenever you print something interesting to stdout , or replace the calls to printf() , fprintf() and the like to write() , which does not buffer. 每当你向stdout打印一些有趣的东西,或者将printf()fprintf()之类的调用替换为write() ,而不是缓冲区。 The downside is that you have to touch the code of the application, of course, but I don't know any way to externally force flushing of an application's output buffers (ie from bash). 缺点是你必须触摸应用程序的代码,当然,但我不知道从外部强制刷新应用程序的输出缓冲区(即来自bash)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM