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 ?

1
  • 2
    Does the python script terminate on error and continue running until you kill it otherwise? Then buffering may be the cause (then try python -u /path/python-script.py > /path/file.log &). Commented Feb 12, 2013 at 13:01

1 Answer 1

0

Encountered similar problem in startup scripts, where I needed to log output. In the end I came up with following:

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 "

Handy as you can also have PID of the process available. Example writes to syslog, but if it is to write to the file use cat:

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

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

It starts a new shell and ties all outputs to command inside exec.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.