Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Supervisor how to log the standard output -

I'm not expert about python, could someone explain where is the problem?

I'd like to collect the stdout through supervisor http://supervisord.org/

I've made 3 different scripts that print output, for bash and PHP I can collect the output, without problems, python doesn't work.

php_test.sh

#!/usr/bin/php
<?php
    for($i=0;$i<100000;$i++){
        sleep(5);
        echo 'test';
    }
?>

bash_test.sh

#!/bin/bash
for i in `seq 1 100000`; do
    sleep 5
    echo item: $i
done

python_test.sh ( with differents tests to print output )

#!/usr/bin/python3
import time
import sys
from contextlib import redirect_stdout

for num in range(0,100000):
    time.sleep(5)
    print("test!")
    sys.stdout.write("test")
    with redirect_stdout(sys.stderr):
        print("test")

My supervisor config files

bash

[program:bash_test]
process_name=bash_test
command=/home/user/bash_test.sh
stdout_logfile=/home/user/bash_test_output.log
stdout_logfile_maxbytes=0

php

[program:php_test]
process_name=php_test
command=/home/user/php_test.sh
stdout_logfile=/home/user/php_test_output.log
stdout_logfile_maxbytes=0

python

[program:python_test]
process_name=python_test
command=/home/user/python_test.sh
stdout_logfile=/home/user/python_test_output.log
stdout_logfile_maxbytes=0

Thank so much for the help. It's driving me crazy ;[

like image 625
Dario Avatar asked Sep 16 '16 11:09

Dario


2 Answers

Python output is buffered, use this after print

sys.stdout.flush()

or (Python 3)

print(something, flush=True)

or better

import logging
logging.warning('Watch out!')

https://docs.python.org/3/howto/logging.html

like image 127
Dario Avatar answered Oct 18 '22 05:10

Dario


You can also install a stdout friendly version using pip install supervisor-stdout . Find the usage instructions here.

UPDATE: you can also update your supervisord.conf to point the output to stdout.

[program:worker2]
command=bash yourscript.sh
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
like image 37
markroxor Avatar answered Oct 18 '22 05:10

markroxor