1

I'm currently executing a python file with runuser and redirecting the output to a file. This is the command:

runuser -l "user" -c "/path/python-script.py parameter1 > /path/file.log &"

This run correctly the python script but creates an empty log file. If I run without the redirect:

runuser -l "user" -c "/path/python-script.py parameter1 &"

runs correctly the python script and make all output from python script to flow the screen. The output from the python script are done with print which output to stdout.

I don't understand why the output from the python script is not dumped to the file. File permissions are correct. The log files is created, but not filled.

But, if I remove the "parameter1", then the error message reported by the python script is correctly dumped to the log file:

runuser -l "user" -c "/path/python-script.py > /path/file.log &"

The error message is done with print too, so I don't understand why one message are dumped and others not.

Maybe runuser interprets the "parameter1" as a command or something. But, the parameter is correctly passed to the script, as I can see with ps:

/usr/bin/python /path/python-script.py connect

I've tried adding 2>&1 but still don't work.

Any idea ?

4

1 回答 1

0

在启动脚本中遇到了类似的问题,我需要记录输出。最后我想出了以下内容:

USR=myuser    
PRG=myprogrog
WKD="/path/to/workdir"
BIN="/path/to/binary"
ARG="--my arguments"
PID="/var/run/myapp/myprog.pid"

su -l $USR -s /bin/bash -c "exec > >( logger -t $PRG ) 2>&1 ; cd $WKD; { $BIN $ARG & }; echo \$! > $PID "

方便,因为您还可以拥有可用进程的 PID。示例写入 syslog,但如果要写入文件,请使用 cat:

LOG="/path/to/file.log"

su -l $USR -s /bin/bash -c "exec > >( cat > $LOG ) 2>&1 ; cd $WKD; { $BIN $ARG & }; echo \$! > $PID "

它启动一个新的 shell 并将所有输出绑定到 exec 中的命令。

于 2013-09-18T08:37:35.757 回答