[英]How to redirect stderr and stdout into /var/log directory in background process?
With the below command ,all stderr and stdout redirect into /tmp/ss.log and it perform in background process. 使用以下命令,所有stderr和stdout都会重定向到/tmp/ss.log,并在后台进程中执行。
python sslocal -c /etc/shadowsocks.json > /tmp/ss.log 2>&1 &
Now to redirect stderr and stdout into /var/log directory as following. 现在将stderr和stdout重定向到/ var / log目录,如下所示。
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. 我用sudo tee试了一下如下。
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? 如何在后台进程中将stderr和stdout重定向到/ var / log目录?
Although you try to redirect stdout
/ stderr
using bash redirection, I may add another alternative: Redirect within your code : 虽然您尝试使用bash重定向重定向stdout
/ stderr
,但我可以添加另一种方法: 在代码中重定向 :
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. 您只需要在应用程序启动期间执行此代码,并将所有输出( stdout
和stderr
)写入定义的日志文件。
只需以root身份调用重定向:
sudo sh -c 'python sslocal -c /etc/shadowsocks.json > /var/log/ss.log 2>&1' &
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
To start it after editing the config file. 在编辑配置文件后启动它。
sudo systemctl daemon-reload
sudo systemctl enable ss.service
sudo systemctl start ss.service
sudo systemctl status ss -l
1.ss run as a service and it start in reboot automatically. 1.ss作为服务运行,它会自动重启。
2.ss can write log into /var/log/ss.log without permission problem. 2.ss可以在没有权限问题的情况下将日志写入/var/log/ss.log。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.