K
K
Kreichik2021-10-16 17:32:30
Python
Kreichik, 2021-10-16 17:32:30

How can I run a Python script in the background and write all the data from the console to a separate file?

There is a question! How to run a script in the background, but at the same time make sure that everything that is output to the console is written to a separate file. I'm running Ubuntu 18.04 via SSH server. I'm running a Python bot script for Telegram. I do this with the command: python bot.py & > log.txt and the script runs in the background, but the errors that are written to the console are not written to the log.txt file

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sergey, 2021-10-17
@Kreichik

ampersand to execute in background must be the last character in command
to concatenate stderr and stdout use &>
example

#!/usr/bin/env python3

from __future__ import print_function
import sys
from time import sleep

print('print to stdout')
sleep(2)
print('print to stderr', file = sys.stderr)

python w.py &>w.log &
[1] 4591
[1]+  Done                    python w.py &> w.log

cat w.log
print to stderr
print to stdout

V
vaut, 2021-10-16
@vaut

Add a stderror redirect to stdout:
2>&1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question