With the below command ,all stderr and stdout redirect into /tmp/ss.log and it perform in background process.
python sslocal -c /etc/shadowsocks.json > /tmp/ss.log 2>&1 &
Now to redirect stderr and stdout into /var/log directory as following.
python sslocal -c /etc/shadowsocks.json > /var/log/ss.log 2>&1 &
bash: /var/log/ss.log: Permission denied
It encounter permission problem.
I made a try with sudo tee as following.
python sslocal -c /etc/shadowsocks.json |sudo tee -a /var/log/ss.log 2>&1 &
python sslocal -c /etc/shadowsocks.json 2>&1|sudo tee -a /var/log/ss.log &
nohup python sslocal -c /etc/shadowsocks.json |sudo tee -a /var/log/ss.log 2>&1 &
nohup python sslocal -c /etc/shadowsocks.json 2>&1|sudo tee -a /var/log/ss.log &
All of them encounter another problem,the command can't run in background process,it run as foreground process.
How to redirect stderr and stdout into /var/log directory in background process?
Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout , use the 2>&1 or &> constructs.
Discussion. &> or >& is a shortcut that simply sends both STDOUT and STDERR to the same place—exactly what we want to do.
The way we can redirect the output is by closing the current file descriptor and then reopening it, pointing to the new output. We'll do this using the open and dup2 functions. There are two default outputs in Unix systems, stdout and stderr. stdout is associated with file descriptor 1 and stderr to 2.
Redirecting stdout and stderr to a file: The I/O streams can be redirected by putting the n> operator in use, where n is the file descriptor number. For redirecting stdout, we use “1>” and for stderr, “2>” is added as an operator. We have created a file named “sample.
Although you try to redirect stdout
/ stderr
using bash redirection, I may add another alternative: Redirect within your code:
import sys
sys.stdout = open(stdout.log, 'w')
sys.stderr = open(stderr.log, 'w')
You just need to execute this code during application startup and all the output (stdout
, and stderr
) will be written to the defined log files.
Just invoke the redirection as root:
sudo sh -c 'python sslocal -c /etc/shadowsocks.json > /var/log/ss.log 2>&1' &
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With