1

I have a Python 3 script that has logging enabled (for debugging purposes). Whenever I run/execute this script it is working fine and the logfiles are created.

I've also created a setup.sh-bash script to download and install my application. It then automatically starts the script by using nohup, in order to make it run on the background. This command is followed by > /dev/null 2>&1 & so it does not output anything to the console. However, this 'bugs' the logging-module from Python, as it can no longer output something to the log-files. It outputs to /dev/null if I'm correct.

I've searched online in order to come up with a solution, but I failed on every try. I hope someone can come up with an idea to this. Maybe I should use something else to make my script run forever (until reboot of course :)) with the possibility to use the Python 3 logging-module and to have no output in the console.

The exact command I'm using to start my script is as follows:

su -l -s /bin/bash scriptuser -c 'nohup python3 '/etc/scriptroot/main.py > /dev/null 2>&1 &'

Thanks in advance

2 Answers 2

2

I am very 'sorry' to say that I have now found the solution myself. It seems that I did not dig deep enough. I am now using daemon-start-stop supported by most UNIX systems, and it's working as it should.

I followed a tutorial on the internet which can be found here: http://blog.scphillips.com/2013/07/getting-a-python-script-to-run-in-the-background-as-a-service-on-boot/

I hope this helps with a lot of other users having the same problem/searching for the same answer.

Thansk for reading.

0

This issue is a result of Python stdout being line buffered. If you run the command with the -u flag it will with unbuffered stdout and stderr:

su -l -s /bin/bash scriptuser -c 'nohup python3 -u '/etc/scriptroot/main.py > /dev/null 2>&1 &'

1
  • You cqn improve your answer by quoting and linking to a citation for this? Aug 15, 2023 at 20:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .