How to make shell output redirect ( ) write while script is still running?
You need to flush the output sys.stdout.flush()
(or smth) if you want to see it immediately. See this
How to redirect output of an entire shell script within the script itself?
Addressing the question as updated.
#...part of script without redirection...
{
#...part of script with redirection...
} > file1 2>file2 # ...and others as appropriate...
#...residue of script without redirection...
The braces '{ ... }' provide a unit of I/O redirection. The braces must appear where a command could appear - simplistically, at the start of a line or after a semi-colon. (Yes, that can be made more precise; if you want to quibble, let me know.)
You are right that you can preserve the original stdout and stderr with the redirections you showed, but it is usually simpler for the people who have to maintain the script later to understand what's going on if you scope the redirected code as shown above.
The relevant sections of the Bash manual are Grouping Commands and I/O Redirection. The relevant sections of the POSIX shell specification are Compound Commands and I/O Redirection. Bash has some extra notations, but is otherwise similar to the POSIX shell specification.
How to redirect and append both standard output and standard error to a file with Bash
cmd >>file.txt 2>&1
Bash executes the redirects from left to right as follows:
>>file.txt
: Openfile.txt
in append mode and redirectstdout
there.2>&1
: Redirectstderr
to "wherestdout
is currently going". In this case, that is a file opened in append mode. In other words, the&1
reuses the file descriptor whichstdout
currently uses.
Force flushing of output to a file while bash script is still running
bash itself will never actually write any output to your log file. Instead, the commands it invokes as part of the script will each individually write output and flush whenever they feel like it. So your question is really how to force the commands within the bash script to flush, and that depends on what they are.
Output redirection in shell script not working completely
as sjsam has answered remove the backticks . I think the syntax is like this
./test >/dev/null 2>&1 << end
source(scripts/all.src);
quit;
end
Redirect all output to file in Bash
That part is written to stderr, use 2>
to redirect it. For example:
foo > stdout.txt 2> stderr.txt
or if you want in same file:
foo > allout.txt 2>&1
Note: this works in (ba)sh, check your shell for proper syntax
How do I use sudo to redirect output to a location I don't have permission to write to?
Your command does not work because the redirection is performed by your shell which does not have the permission to write to /root/test.out
. The redirection of the output is not performed by sudo.
There are multiple solutions:
Run a shell with sudo and give the command to it by using the
-c
option:sudo sh -c 'ls -hal /root/ > /root/test.out'
Create a script with your commands and run that script with sudo:
#!/bin/sh
ls -hal /root/ > /root/test.outRun
sudo ls.sh
. See Steve Bennett's answer if you don't want to create a temporary file.Launch a shell with
sudo -s
then run your commands:[nobody@so]$ sudo -s
[root@so]# ls -hal /root/ > /root/test.out
[root@so]# ^D
[nobody@so]$Use
sudo tee
(if you have to escape a lot when using the-c
option):sudo ls -hal /root/ | sudo tee /root/test.out > /dev/null
The redirect to
/dev/null
is needed to stop tee from outputting to the screen. To append instead of overwriting the output file
(>>
), usetee -a
ortee --append
(the last one is specific to GNU coreutils).
Thanks go to Jd, Adam J. Forster and Johnathan for the second, third and fourth solutions.
Assign output of os.system to a variable and prevent it from being displayed on the screen
From "Equivalent of Bash Backticks in Python", which I asked a long time ago, what you may want to use is popen
:
os.popen('cat /etc/services').read()
From the docs for Python 3.6,
This is implemented using subprocess.Popen; see that class’s
documentation for more powerful ways to manage and communicate with
subprocesses.
Here's the corresponding code for subprocess
:
import subprocess
proc = subprocess.Popen(["cat", "/etc/services"], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
print "program output:", out
Related Topics
Python Multiprocessing Pool.Apply_Async with Shared Variables (Value)
Binding Callbacks to Minimize and Maximize Events in Toplevel Windows
Pyodbc:Can't Open the Driver Even If It Exists
Multiprocessing Module Showing Memory for Each Child Process Same as Main Process
What Are the Tkinter Events for Horizontal Edge Scrolling (In Linux)
Paramiko Error: Error Reading Ssh Protocol Banner
A Simple Python Deployment Problem - a Whole World of Pain
Create Single Python Executable Module
Differencebetween C.Utf-8 and En_Us.Utf-8 Locales
How to Default Python3.8 on My MAC Using Homebrew
How to Get a Process's Stdin by a Process Id
How to Deal with Linux/Python Dependencies
How to Extract a Floating Number from a String