Hot answers tagged

84

You can use tail command with -f : tail -f /var/log/syslog It's good solution for real time show.


83

I don't think you're going to get any more elegant than the tail -f /dev/null that you already suggested (assuming this uses inotify internally, there should be no polling or wakeups, so other than being odd looking, it should be sufficient). You need a utility that will run indefinitely, will keep its stdout open, but won't actually write anything to ...


75

POSIX defines standard error as for writing diagnostic output This doesn't limit its use to error messages only. I would consider progress information as diagnostic output, so it belongs on standard error.


73

This is happening because normally when process STDOUT is redirected to something other than a terminal, then the output is buffered into some OS-specific-sized buffer (perhaps 4k or 8k in many cases). Conversely, when outputting to a terminal, STDOUT will be line-buffered or not buffered at all, so you'll see output after each \n or for each character. ...


50

sleep infinity is the clearest solution I know of. You can use infinity because sleep accepts a floating point number*, which may be decimal, hexadecimal, infinity, or NaN, according to man strtod. * This isn't part of the POSIX standard, so isn't as portable as tail -f /dev/null. However, it is supported in GNU coreutils (Linux) and BSD (used on Mac) (...


48

This should do the job: import time, sys for i in range(10): print('bla') sys.stdout.flush() time.sleep(5) As Python will buffer the stdout by default, here i have used sys.stdout.flush() to flush the buffer. Another solution would be to use the -u(unbuffered) switch of python. So, the following will do too: python -u script.py >> log


44

The most obvious one is cat. But, also have a look at head and tail. There are also other shell utillities to print a file line by line: sed, awk, grep. But those are to alternate the file content or to search inside the file. I made a few tests to estimate which is the most effective one. I run all trough strace to see which made the least system calls. My ...


44

echo An echo implementation which strictly conforms to the Single Unix Specification will add newlines if you do: echo 'line1\nline2' But that is not a reliable behavior. In fact, there really isn't any standard behavior which you can expect of echo. OPERANDS string A string to be written to standard output. If the first ...


43

I don't think this is entirely a bash issue. In a comment, you said that you saw this error after doing sudo su username2 when logged in as username. It's the su that's triggering the problem. /dev/stdout is a symlink to /proc/self/fd/1, which is a symlink to, for example, /dev/pts/1. /dev/pts/1, which is a pseudoterminal, is owned by, and writable by, ...


43

vipe is a program for editing pipelines: command1 | vipe | command2 You get an editor with the complete output of command1, and when you exit, the contents are passed on to command2 via the pipe. In this case, there's no command1. So, you could do: : | vipe | pandoc -o foo.pdf Or: vipe <&- | pandoc -o foo.pdf vipe picks up on the EDITOR and ...


42

Problem is that when you redirect your output, it's not available anymore for the next redirect. You can pipe to tee in a subshell to keep the output for the second redirection: ( cmd | tee -a file2 ) >> file1 2>&1 or if you like to see the output in terminal: ( cmd | tee -a file2 ) 2>&1 | tee -a file1 To avoid adding the stderr of ...


37

Just pipe the command into a while loop. There are a number of nuances to this, but basically (in bash or any POSIX shell): longcommand | while IFS= read -r line do whatever "$line" done The other main gotcha with this (other than the IFS stuff below) is when you try to use variables from inside the loop once it has finished. This is because the ...


32

You need to combine the output of STDERR and STDOUT prior to piping it to logger. Try this instead: /home/dirname/application_name -v 2>&1 | logger & Example $ echo "hi" 2>&1 | logger & [1] 26818 [1]+ Done echo "hi" 2>&1 | logger $ sudo tail /var/log/messages Apr 12 17:53:57 greeneggs saml: hi You can use ...


31

I want to see output of first command in terminal, and save the output of the second command in a file. As long as you don't care whether what you are looking at is from stdout or stderr, you can still use tee: myscript | tee /dev/stderr | grep -P 'A|C' > out.file Will work on linux; I don't know if "/dev/stderr" is equally applicable on other *nixes.


31

Pass the -e flag to echo to make it parse escaped characters such as "\r\n", like so: echo -e "Line 1\r\nLine2" >> readme.txt


29

You can do this with sponge from moreutils. sponge will "soak up standard input and write to a file". With no arguments, that file is standard output. Input given to this command is stored in memory until EOF, and then written out all at once. For writing to a normal file, you can just give the filename: cmd | sponge filename The main purpose of sponge is ...


28

Posix defines the standard streams thus: At program start-up, three streams shall be predefined and need not be opened explicitly: standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). When opened, the standard error stream is not fully buffered; the standard ...


26

A history of toolsets You are not the first person to want such a tool. People have been wanting such tools for 30 years. And they've existed for almost that long, too. The earliest tool for this sort of thing was Daniel J. Bernstein's "pty" package, described by Rich Salz as a "Ginsu knife", which Bernstein wrote back at the turn of the 1990s in order ...


25

You are confusing $(…) with <(…). You used the former, which passes the output as arguments to vimdiff. For example, if the last line of /path/to/foo contains bar bar bar, then the following command echo $(tail -1 /path/to/foo) is equivalent to echo bar bar bar Instead, you need to use <(…). This is called process substitution, and passes the ...


24

You can stop both processing by sending them SIGSTOP (replace pid1 and pid2 by the actual PIDs or use killall and the application name): kill -SIGSTOP pid1 pid2 The printing on the terminal (or wherever stdout is redirected to) should stop. Then continue one of them using kill -SIGCONT pid1 If the error messages appear immediately, you know its the ...


23

You might get what you need by using unbuffer. unbufferis a tcl / expect script. Look at the source if you want. Also note the CAVEATS section in man. Also note that it does not execute aliases such as: alias ls='ls --color=auto' unless one add a trick as noted by Stéphane Chazelas: If you do a alias unbuffer='unbuffer ' (note the trailing space), then ...


23

You can use: vim -c 'set syntax=sql' -


23

They do interleave! You only tried short output bursts, which remain unsplit, but in practice it's hard to guarantee that any particular output remains unsplit. Output buffering It depends how the programs buffer their output. The stdio library that most programs use when they're writing uses buffers to make output more efficient. Instead of outputting ...


22

I know cat can do this, but its main purpose is to concatenate rather than just displaying the content. The purpose of cat is exactly that, read a file and output to stdout.


22

What happens when you do some_command >>file 2>>file is that file will be opened for appending twice. This is safe to do on a POSIX filesystem. Any write that happens to the file when it's opened for appending will occur at the end of the file, regardless of whether the data comes over the standard output stream or the standard error stream. ...


22

No, it's not just as safe as the standard >>bar 2>&1. When you're writing foo >>bar 2>>bar you're opening the bar file twice with O_APPEND, creating two completely independent file objects[1], each with its own state (pointer, open modes, etc). This is very much unlike 2>&1 which is just calling the dup(2) system call, and ...


21

You can use socat to start your process with a pty connected, and get socat to connect the other end of the pty to a file. Which AFAIU is exactly what you asked: socat EXEC:"my-command",pty GOPEN:mylog.log This method will cause isatty called by my-command to return true and a process that relies only on that will be fooled to output control codes. Note ...


21

You can do this from within Vim: :w !pandoc -o file.pdf Or even write the buffer into a complex pipeline: :w !grep pattern | somecommand > file.txt And then you can exit Vim without saving: :q! However, considering your specific use case, there is probably a better solution by using vi as your command line editor. Assuming you use bash: set -o vi ...


20

If you want to show a short file, that fits on one terminal screen, and what is changing is possibly the whole file, you could use watch: watch cat example.txt Every 2.0s: cat example.txt Sun Aug 3 15:25:20 2014 Some text another line It shows the whole file every 2 seconds by default, including an optional header: The ...


Only top voted, non community-wiki answers of a minimum length are eligible