stderr

Append text to stderr redirects in bash

本秂侑毒 提交于 2019-11-28 06:33:43
Right now I'm using exec to redirect stderr to an error log with exec 2>> ${errorLog} The only downside is that I have to start each run with a timestamp since exec just pushes the text straight into the log file. Is there a way to redirect stderr but allow me to append text to it, such as a time stamp? This is very interesting. I've asked a guy who knows bash quite well, and he told me this way: foo() { while IFS='' read -r line; do echo "$(date) $line" >> file.txt; done; }; First, that creates a function reading one line of raw input from stdin, while the assignment to IFS makes it doesn't

标准I/O与管道

半城伤御伤魂 提交于 2019-11-28 05:07:24
标准I/O与管道 标准输入和输出 读入数据:Input 输出数据:Output Linux给程序提供三种I/O设备 标准输入(STDIN)-0 默认接受来自键盘的输入 标准输出(STDOUT)-1 默认输出到终端窗口 标准错误(STDERR)-2 默认输出到终端窗口 I/O重定向:改变默认位置 把输出和错误重新定向到文件 STDOUT和STDERR可以被重定向到文件 命令 ~ 操作符号 ~ 文件名 支持的操作符号包括: 把STDOUT重定向到文件> 2> 把STDERR重定向到文件 &> 把所有输出重定向到文件 使用>文件内容会被覆盖 set -C 禁止将内容覆盖已有文件,但可追加 使用>| file 强制覆盖 set +C 允许覆盖 使用>>原有内容基础上,追加内容 把输出和错误重新定向到文件 2> 覆盖重定向错误输出数据流 2>> 追加重定向错误输出数据流 标准输出和错误输出各自定向至不同位置 COMMAND > /path/to/file.out 2> /path/to/error.out 合并标准输出和错误输出为同一个数据流进行重定向 &> 覆盖重定向 &>> 追加重定向 COMMAND > /path/to/file.out 2>&1 (顺序很重要) COMMAND >> /path/to/file.out 2>&1 ():合并多个程序的STDOUT ( cal 2007

When should I use perror(“…”) and fprintf(stderr, “…”)?

╄→гoц情女王★ 提交于 2019-11-28 02:49:45
Reading the man pages and some code did not really help me in understanding the difference between - or better, when I should use - perror("...") or fprintf(stderr, "...") . Jason Calling perror will give you the interpreted value of errno , which is a thread-local error value written to by POSIX syscalls (i.e., every thread has it's own value for errno ). For instance, if you made a call to open() , and there was an error generated (i.e., it returned -1 ), you could then call perror immediately afterwards to see what the actual error was. Keep in mind that if you call other syscalls in the

Copy STDOUT to file without stopping it showing onscreen

女生的网名这么多〃 提交于 2019-11-28 01:26:08
The program I am making is designed to be run unattended, because of this I have redirected the stdout and stderr streams to a log file. While this works without any problems, while I am still making and debugging the software I would like it to show on the screen as well. Is this possible? To redirect the streams I have used System.setErr(logWriter); System.setOut(logWriter); Thanks. a bit crude perhaps, but you could try this: private static final isDebugMode = true; ... if (!isDebugMode) { System.setErr(logWriter); System.setOut(logWriter); } Alternatively you could write your own

Docker容器日志管理介绍

时间秒杀一切 提交于 2019-11-28 00:06:08
Docker容器日志分为2类: Docker引擎日志(Docker本身运行的日志)。 容器日志,各个容器内产生的日志。 Docker引擎日志 Centos系统下Docker引擎log一般给systemd管理,可通过 journalctl -u docker.service 命令查看。 容器日志 一、查看日志命令 docker logs 容器ID 显示当前运行容器的log,输出Linux下的STDOUT(标准输出)、STDERR(标准错误输出),docker logs 显示的内容包含STDOUT和STDERR。生产环境中,如果应用输出到我们自己的日志文件里,使用docker logs一般收集不到太多重要信息,大多有用log在容器内部。 docker logs 容器ID 或 -f 参数 实施查看log,类似Linux的tail -f。如: [root@docker-qa logs]# docker ps |grep part ed9e7d9df4b1 b2b-partner-img/b2b-partner-test "/bin/sh -c 'java ..." 45 minutes ago Up 45 minutes 0.0.0.0:8081->8081/tcp b2b-partner-test-container [root@docker-qa logs]# [root@docker

使用struct模块解决黏包问题

不羁岁月 提交于 2019-11-27 21:42:04
1 ''' 2 远程执行cmd命令程序 3 如果不使用struct模块,ipconfig等会返回长数据的命令会发生黏包现象 4 ''' 5 import socket 6 import struct 7 8 sk = socket.socket() 9 sk.bind(("127.0.0.1", 8080)) 10 sk.listen() 11 conn, addr = sk.accept() 12 while True: 13 cmd = input(">>>") 14 if cmd == "q": 15 conn.send(b"q") 16 break 17 conn.send(bytes(cmd,encoding="gbk")) 18 # windows系统的命令行的编码格式是GBK所以要进行GBK转码 19 num = conn.recv(4) 20 # 接收返回数据的长度 21 num = int(struct.unpack("i",num)[0]) 22 # 将接收的bytes类型的struct转换的数字解码,因为返回的是元组所以取元组的第一个,并且下面要用的类型是整数型,转换成int类型 23 print(conn.recv(num).decode("gbk")) 24 # 接收上面传进来大小的数据,解码gbk,打印 25 # 发送多少接收多少就不会产生黏包现象了

python利用socket建立通讯

拜拜、爱过 提交于 2019-11-27 21:17:36
链接+循环 通信 服务端常驻 server端 import socket phone=socket.socket()#买手机 phone.bind(('127.0.0.1',6666))#绑卡 phone.listen(5)#设置连接数一个连接加5个等待 缓存池 while 1: conn,addr=phone.accept() print(f'用户{addr}来了') while 1: try: from_client_data=conn.recv(1024) if from_client_data.decode('utf-8')=='Q': print('正常断开') break print(f'来自客户端{addr}消息{from_client_data.decode("utf-8")}') to_client_data=input('>>>>>>>') conn.send(to_client_data.encode('utf-8')) except Exception: break conn.close() phone.close() client端 # 自己的 import socket phone=socket.socket() phone.connect(('127.0.0.1',6666)) while 1: to_server_data=input('>>>>>

Understanding stdin stdout stderr [duplicate]

孤街醉人 提交于 2019-11-27 21:16:29
This question already has an answer here: Confused about stdin, stdout and stderr? 10 answers I'm trying to understand stdin stdout and stderr . I see them used in people's code all the time and I can't understand exactly what they are. I am assuming that they have something to do with input/output but have been searching for an explanation online and can't find one. Does anybody know of a good link with an explanation or if it is simple enough to explain it would be a great help to me. Since I am learning Python 3, examples in that would be helpful. sys.stdin sys.stdout sys.stderr File

Wrap subprocess' stdout/stderr

≯℡__Kan透↙ 提交于 2019-11-27 20:53:26
I'd like to both capture and display the output of a process that I invoke through Python's subprocess. I thought I could just pass my file-like object as named parameter stdout and stderr I can see that it accesses the fileno attribute - so it is doing something with the object. However, the write() method is never invoked. Is my approach completely off or am I just missing something? class Process(object): class StreamWrapper(object): def __init__(self, stream): self._stream = stream self._buffer = [] def _print(self, msg): print repr(self), msg def __getattr__(self, name): if not name in [

How to capture stderr on Windows/DOS?

可紊 提交于 2019-11-27 20:19:55
问题 I want to capture the errors from a script into a file instead of to the screen. In *nix, this is done with stderr redirection, usually echo "Error" 2> errorfile.log How do I do it in a CMD script under Windows? 回答1: For example: PSKILL NOTEPAD >output.txt 2>&1 This will direct stdout and stderr to a file name output.txt. See Underused features of Windows batch files for more details. 回答2: That should work in Win32, too. If you have already redirected stdout, and want stderr redirected to the