stdout

fork() and printf()

百般思念 提交于 2019-12-08 04:37:45
问题 As I understood fork() creates a child process by copying the image of the parent process. My question is about how do child and parent processes share the stdout stream? Can printf() function of one process be interrupted by other or not? Which may cause the mixed output. Or is the printf() function output atomic? For example: The first case: parent: printf("Hello"); child: printf("World\n"); Console has: HeWollorld The second case: parent: printf("Hello"); child: printf("World\n"); Console

python: Using ncurses when underlying library logs to stdout

不羁的心 提交于 2019-12-08 03:54:39
问题 I am trying to write a small python program that uses curses and a SWIGed C++ library. That library logs a lot of information to STDOUT, which interferes with the output from curses. I would like to somehow intercept that content and then display it nicely through ncurses. Is there some way to do this? 回答1: A minimal demonstrating example will hopefully show how this all works. I am not going to set up SWIG just for this, and opt for a quick and dirty demonstration of calling a .so file

How can a process intercept stdout and stderr of another process on Linux?

十年热恋 提交于 2019-12-08 03:12:17
问题 I have some scripts that ought to have stopped running but hang around forever. Is there some way I can figure out what they're writing to STDOUT and STDERR in a readable way? I tried, for example, to do: $ tail -f /proc/(pid)/fd/1 but that doesn't really work. It was a long shot anyway. Any other ideas? strace on its own is quite verbose and unreadable for seeing this. Note: I am only interested in their output, not in anything else. I'm capable of figuring out the other things on my own;

Printing output in realtime from subprocess

时间秒杀一切 提交于 2019-12-08 02:32:38
问题 I'm trying to print stdout in realtime for a subprocess but it looks like stdout is buffered even with bufsize=0 and I can't figure out how to make it work, I always have a delay. The code I tried : p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=0) line = p.stdout.readline() while line: sys.stdout.write(line) sys.stdout.flush() # DO OTHER STUFF line = p.stdout.readline() Also tried with for line in iter(p.stdout.readline, b'') instead of the while loop and

Streaming stdout to a web page

萝らか妹 提交于 2019-12-07 22:13:28
问题 This seems like it should be a really simple thing to achieve, unfortunately web development was never my strong point. I have a bunch of scripts, and I would like to launch them from a webpage and see the realtime stdout text on the page. Some of the scripts take a long time to run so the normal single response isn't good enough (I have this working already). As far as I can see, my options are stdout to a file, and periodically (every couple of seconds) send a request from the client and

PBSPro qsub output error file directed to path with jobid in name

房东的猫 提交于 2019-12-07 19:15:29
I'm using PBSPro and am trying to use qsub command line to submit a job but can't seem to get the output and error files to be named how I want them. Currently using: qsub -N ${subjobname_short} \ -o ${path}.o{$PBS_JOBID} -e ${path}.e${PBS_JOBID} ... submission_script.sc Where $path=fulljobname (i.e. more than 15 characters) I'm aware that $PBS_JOBID won't be set until after the job is submitted... Any ideas? Thanks The solution I came up with was following the qsub command with a qalter command like so: jobid=$(qsub -N ${subjobname_short} submission_script.sc) qalter -o ${path}.o{$jobid} -e $

Capture stdout to a string and output it back to stdout in C

怎甘沉沦 提交于 2019-12-07 18:52:02
问题 C language is used. I have a function that writes to stdout. I would like to capture that output, modify it a bit (replacing some strings). And than output it again to the stdout. So I want to start with: char huge_string_buf[MASSIVE_SIZE]; freopen("NUL", "a", stdout); -OR- freopen("/dev/null", "a", stdout); setbuf(stdout, huge_string_buffer); /* modify huge_string_buffer */ The question is now, how do I output the huge_string_buffer back to the original stdout? 回答1: One idea is to mimic the

How to get stdout into a string (Python) [duplicate]

别来无恙 提交于 2019-12-07 18:49:58
问题 This question already has answers here : Store output of subprocess.Popen call in a string (14 answers) Closed 5 years ago . I need to capture the stdout of a process I execute via subprocess into a string to then put it inside a TextCtrl of a wx application I'm creating. How do I do that? EDIT: I'd also like to know how to determine when a process terminates 回答1: From the subprocess documentation: from subprocess import * output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0] 回答2:

Problem redirecting stdout in Ruby script

感情迁移 提交于 2019-12-07 18:32:32
问题 I have the following test Ruby script: require 'tempfile' tempfile = Tempfile.new 'test' $stderr.reopen tempfile $stdout.reopen tempfile puts 'test stdout' warn 'test stderr' `mail -s 'test' my@email.com < #{tempfile.path}` tempfile.close tempfile.unlink $stderr.reopen STDERR $stdout.reopen STDOUT The email that I get has the contents: test stderr Why is stderr redirecting properly but not stdout? Edit: In response to a comment I added a $stdout.flush after the puts line and it printed

difference between communicate() and .stdin.write, .stdout.read or .stderr.read - python

谁说我不能喝 提交于 2019-12-07 17:49:12
问题 I wan to create a pipe between 3 commands: cat = subprocess.Popen("cat /etc/passwd", stdout=subprocess.PIPE) grep = subprocess.Popen("grep '<usernamr>'", stdin=cat.stdout, stdout=subprocess.PIPE) cut = subprocess.Popen("cut -f 3 -d ':'", stdin=grep.stdout, stdout=subprocess.PIPE) for line in cut.stdout: # process each line here But python documentation says: Use communicate() rather than .stdin.write , .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers