How to redirect stderr and stdout to / var / log directory in the background?

With the following command, all stderr and stdout are redirected to /tmp/ss.log and run in the background.

python sslocal -c /etc/shadowsocks.json > /tmp/ss.log 2>&1 & 

Now to redirect stderr and stdout to the / var / log directory as follows.

 python sslocal -c /etc/shadowsocks.json > /var/log/ss.log 2>&1 & bash: /var/log/ss.log: Permission denied 

He is facing a resolution problem.
I tried sudo tee as shown below.

 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 & 

They all face a different problem, the team cannot work in the background, it starts as a foreground process.

How to redirect stderr and stdout to / var / log directory in the background?

+5
source share
3 answers
 sudo vi /etc/systemd/system/ss.service [Unit] Description=ss [Service] TimeoutStartSec=0 ExecStart=/bin/bash -c '/python sslocal -c /etc/ss.json > /var/log/ss.log 2>&1' [Install] WantedBy=multi-user.target 

Run it after editing the configuration file.

 sudo systemctl daemon-reload sudo systemctl enable ss.service sudo systemctl start ss.service sudo systemctl status ss -l 

1.ss starts as a service and starts automatically upon reboot.
2.ss can write a log to /var/log/ss.log without resolving the problem.

+2
source

Although you are trying to redirect stdout / stderr using bash redirection, I can add another alternative: Redirect to 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 output ( stdout and stderr ) will be written to specific log files.

+3
source

Just call the root redirect:

 sudo sh -c 'python sslocal -c /etc/shadowsocks.json > /var/log/ss.log 2>&1' & 
+2
source

Source: https://habr.com/ru/post/1266888/


All Articles